0

我有一个名为TestAppwhich extends的类Activity和另一个名为SecondClasswhich extends的类TestApp。在TestApp活动中,我有一个在按钮上设置文本的方法。当我从TestAppto移动SecondClass并调用TestAppfromSecondClass获取NullPointerException按钮对象的方法时。为什么会这样?任何建议都将是可观的。这是我的第一个活动代码。

@Override
public void onCreate(Bundle savedInstanceState) {
    ok = (Button) findViewById(R.id.button1);
    cancel = (Button) findViewById(R.id.button2);
    setTextAgain();
}

protected void setTextAgain() {
    ok.setText("hi");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0, 0, 1, "1");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case 0:
        SecondClass second = new SecondClass(this);
        second.initialize();
        break;
            }
    return super.onOptionsItemSelected(item);
}

这是我的二等代码

public class SecondClass extends TestAppActivity{

Context l_context;

public SecondClass(TestAppActivity p_testAppActivity) {

    l_context = p_testAppActivity;

}

public void initialize() {

    setTextAgain();

}

这是 logCat o/p

02-17 22:43:54.159: E/AndroidRuntime(1246): java.lang.NullPointerException
02-17 22:43:54.159: E/AndroidRuntime(1246):     at       
android.app.Activity.setContentView(Activity.java:1657)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at    
com.sachit.testApp.TestAppActivity.setTextAgain(TestAppActivity.java:25)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at
com.sachit.testApp.SecondClass.initialize(SecondClass.java:17)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at    
com.sachit.testApp.TestAppActivity.onOptionsItemSelected(TestAppActivity.java:46)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
android.app.Activity.onMenuItemSelected(Activity.java:2205)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at 
com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at  
com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at    
android.view.View$PerformClick.run(View.java:9080)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
android.os.Handler.handleCallback(Handler.java:587)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
android.os.Handler.dispatchMessage(Handler.java:92)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
android.os.Looper.loop(Looper.java:123)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at 
android.app.ActivityThread.main(ActivityThread.java:3683)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at 
java.lang.reflect.Method.invokeNative(Native Method)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at   
java.lang.reflect.Method.invoke(Method.java:507)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-17 22:43:54.159: E/AndroidRuntime(1246):     at     
dalvik.system.NativeStart.main(Native Method)
4

0 回答 0