Here is the problem that I am trying to solve, I ask the user to perform some computation in the current activity which I call as activity1 and I ask the user to press a button to perform the computation after the user presses the button I move him to another activity where I need to display the results of the computation performed by the user, but I am unable to set the TextView for the activity, I read several stackoverflow posts and then finally landed on one that I found somewhat relevant - NullPointer Exception when assigning value to TextView. I keep on getting these NullPointerExceptions and I don't know now what to do. Here is the code for activity2 file wherein I want to display the contents of the computation to the user.
package com.example.mathcalculator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class CalculatorActivity3 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = new Bundle();
bundle.putString("key", getIntent().getStringExtra(CalculatorActivity2.EXTRA_MESSAGE));
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setContentView(R.layout.activity_calculator_activity3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator_activity3, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calculator_activity3, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.text_view);
String strtext = getArguments().getString("key");
textView.setText("The result of the computation is :" + strtext);
return rootView;
}
}
}
This is the activity through which I am passing the content:
package com.example.mathcalculator;
import java.math.BigDecimal;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class CalculatorActivity2 extends ActionBarActivity {
// this is the operation requested by the user and we store it in a global variable
public static String operationRequested = "";
// this is the message that is passed along with the result of the computation
public static String EXTRA_MESSAGE = "com.example.mathcalculator.CalculatorActivity2.Message";
// this function is used to perform the requisite operation as requested by the user on the previous screen of the application
public void computeOperation(View view){
// get the numbers from the 2 fields
EditText et1 = (EditText)findViewById(R.id.number_1);
EditText et2 = (EditText)findViewById(R.id.number_2);
String et1String = et1.getText().toString();
String et2String = et2.getText().toString();
// these are the 2 decimal numbers input by the user in the 2 edit text fields
BigDecimal num1 = new BigDecimal(et1String);
BigDecimal num2 = new BigDecimal(et2String);
// make an intent to pass the result to the next activity
Intent intent = new Intent(this,CalculatorActivity3.class);
if(operationRequested.equals("sum")){
// this is the result of the operation
BigDecimal result = num1.add(num2);
Log.e("The operation requested was sum and the result of the operation is : " , result.toString());
// put the key-value pair in the intent
intent.putExtra(EXTRA_MESSAGE, result.toString());
}
else if(operationRequested.equals("difference")){
// this is the result of the operation
BigDecimal result = num1.subtract(num2);
Log.e("The operation requested was difference and the result of the operation is : " , result.toString());
// put the key-value pair in the intent
intent.putExtra(EXTRA_MESSAGE, result.toString());
}
else if(operationRequested.equals("product")){
// this is the result of the operation
BigDecimal result = num1.multiply(num2);
Log.e("The operation requested was product and the result of the operation is : " , result.toString());
// put the key-value pair in the intent
intent.putExtra(EXTRA_MESSAGE, result.toString());
}
else if(operationRequested.equals("division")){
// this is the result of the operation
BigDecimal result = num1.divide(num2);
Log.e("The operation requested was division and the result of the operation is : " , result.toString());
// put the key-value pair in the intent
intent.putExtra(EXTRA_MESSAGE, result.toString());
}
else;
// start the new activity
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator_activity2);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
// this is the intent received from the previous activity
Intent intent = getIntent();
operationRequested = intent.getStringExtra(CalculatorActivity1.EXTRA_MESSAGE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_calculator_activity2, container, false);
return rootView;
}
}
}
and the fragment file for the same activity above
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/calculato_activity_1">
<TextView android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>