我已经尝试了一些提交给堆栈溢出的解决方案,但是到目前为止没有一个对我有帮助。当在调用父活动的另一个片段中触发按钮侦听器时,就会出现问题。父活动调用此fragmentsupdateValue()
方法(见下文)。在片段的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
}
}
最佳答案
空指针可能是因为resultTV
没有显示给updateValue()
方法。如何在fragment类中声明resultTV
?如果要实例化类的多个实例,并且希望它随时间保持不变,则可能需要尝试将其声明为静态的。
你也可以试试
TextView resultTV = (TextView)view.findViewById(R.id.resultTV);
在
updateValue
方法中