我已经尝试了许多提交给堆栈溢出的解决方案,但到目前为止没有一个对我有帮助。当在另一个调用父活动的片段中触发按钮侦听器时,就会出现问题。父活动调用此片段updateValue()
方法(见下文)。在onCreateView()
我的片段中,我有以下代码:
Result.class 扩展片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.result, container, false);
resultTV = (TextView)view.findViewById(R.id.resultTV);
resultTV.setText(resultTV.getText().toString() + "test!");
return view;
}
这完美地工作.. 但是,当我从父活动调用片段中的方法时,textview 引用为空(空指针异常)。异常发生在resultTV.setText()
被调用的地方。
public void updateValue(View v) {
String text = ((Button)v).getText().toString();
if(text.toLowerCase() == "c") resultTV.setText(DEFAULT_RESULT_TEXT);
else resultTV.setText(resultTV.getText().toString() + text);
}
例外
10-09 16:32:59.091: E/AndroidRuntime(2324): FATAL EXCEPTION: main
10-09 16:32:59.091: E/AndroidRuntime(2324): java.lang.NullPointerException
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.test.Result.updateValue(Result.java:53)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.test.Main.onBasicButtonClicked(Main.java:65)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.test.Basic$7.onClick(Basic.java:120)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.view.View.performClick(View.java:4240)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.view.View$PerformClick.run(View.java:17721)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.os.Handler.handleCallback(Handler.java:730)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.os.Looper.loop(Looper.java:137)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-09 16:32:59.091: E/AndroidRuntime(2324): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 16:32:59.091: E/AndroidRuntime(2324): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 16:32:59.091: E/AndroidRuntime(2324): at dalvik.system.NativeStart.main(Native Method)
编辑:
Main 有一些片段:basic
片段scientific
只是按钮。该matrix
片段尚未使用,因此我们可以忽略该片段。result
片段是TextView
找到问题的地方。
Main.class(主要活动类)
public class Main extends Activity implements
OnBasicButtonClickedListener,
OnScientificButtonClickedListener,
OnMatrixButtonClickedListener {
private FragmentManager fm = getFragmentManager();
private FragmentTransaction ft = fm.beginTransaction();
private Basic basic = new Basic();
private Scientific scientific = new Scientific();
private Result result = new Result();
private MatrixFragment matrixFrag = new MatrixFragment();
private static final String LOG = "Main.class";
/* this list view will hold all submitted formulas */
private ListView formulaLV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(findViewById(R.id.result) == null)
ft.add(R.id.result, result);
if(findViewById(R.id.basic) == null)
ft.add(R.id.basic, basic);
if(findViewById(R.id.scientific) == null)
ft.add(R.id.scientific, scientific);
if(findViewById(R.id.matrix) == null)
ft.add(R.id.matrix, matrixFrag);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return false;
}
/*
* This method acts as a listener for fragment to fragment communication
* from Basic fragment.
*/
public void onBasicButtonClicked(View v) {
Log.i(LOG, ((Button)v).getText() + " button pressed!");
result.updateValue(v);
}
/*
* This method acts as a listener for fragment to fragment communication
* from Scientific fragment.
*/
public void onScientificButtonClicked(View v) {
Log.i(LOG, ((Button)v).getText() + " button pressed!");
result.updateValue(v);
}
@Override
public void matrixButtonClicked(View view) {
// matrix button clicked
}
}