[英]How to Initialize the TextView to display dynamic content while building an Android application?
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 . 这是我要解决的问题,我要求用户在当前活动中执行一些计算,我将该活动称为Activity1,并要求用户在用户按下按钮后移动我按下他以执行计算到另一个我需要显示用户执行的计算结果的活动,但是我无法为该活动设置TextView,我阅读了几个stackoverflow帖子,然后最终登陆到一个我发现有些相关的帖子-NullPointer Exception将值分配给TextView 。 I keep on getting these NullPointerExceptions and I don't know now what to do.
我一直在获取这些NullPointerExceptions,现在不知道该怎么办。 Here is the code for activity2 file wherein I want to display the contents of the computation to the user.
这是activity2文件的代码,其中我想向用户显示计算内容。
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>
The problem is that you are trying to add a fragment to the transaction before you set the layout for you acitivity. 问题是您要在为活动设置布局之前尝试向事务添加片段。
problem: 问题:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderF()).commit();ragment
}
setContentView(R.layout.activity_calculator_activity3);
As you can see you set the layout after to do a transaction thus it will catch a exception to the .add(R.id.container, new PlaceholderF()).commit();
如您所见,您在进行事务后设置了布局,因此它将捕获
.add(R.id.container, new PlaceholderF()).commit();
的异常.add(R.id.container, new PlaceholderF()).commit();
R.id.container is not yet added so it will return null. R.id.container尚未添加,因此它将返回null。
solution: 解:
add the fragment after you set the layout for your activity 在设置活动的布局后添加片段
setContentView(R.layout.activity_calculator_activity3);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderF()).commit();ragment
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.