2

我已经尝试了许多提交给堆栈溢出的解决方案,但到目前为止没有一个对我有帮助。当在另一个调用父活动的片段中触发按钮侦听器时,就会出现问题。父活动调用此片段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 

}

}

4

1 回答 1

3

空指针可能是因为resultTV没有显示给updateValue()方法。resultTV你是如何在片段类中声明的?如果您要实例化该类的多个实例并且希望它随着时间的推移保持不变,您可能想尝试将其声明为静态

您可能还想尝试

TextView resultTV = (TextView)view.findViewById(R.id.resultTV);

updateValue方法中

于 2013-10-09T21:36:12.843 回答