我正在开发一个应用程序,在我的主要活动中,有一个选项卡主机和一个编辑文本字段。更改选项卡时,选项卡主机将在每个选项卡内加载不同的活动。在每个选项卡中加载的每个活动中,我都放置了一个按钮。当我单击按钮时,我需要在主活动的编辑文本中显示一些文本。但每当我尝试在主活动之外的其他活动中初始化编辑文本字段时,我都会收到“应用程序强制关闭”。
因此,如果有人可以帮助我从选项卡中加载的活动中的按钮中获取编辑文本获取价值,那将是一个很大的帮助
这是屏幕截图蓝色框将是每个选项卡中加载的活动,其中将是主要活动
这就是发生强制关闭时 Logcat 输出的样子。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.batz.voiceforyou/com.batz.voiceforyou.Things}: java.lang.NullPointerException
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1906)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.widget.TabHost.setCurrentTab(TabHost.java:358)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:553)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.view.View.performClick(View.java:4129)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.view.View$PerformClick.run(View.java:17143)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.os.Handler.handleCallback(Handler.java:615)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.os.Looper.loop(Looper.java:137)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.ActivityThread.main(ActivityThread.java:4802)
03-24 17:55:23.928: E/AndroidRuntime(23581): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 17:55:23.928: E/AndroidRuntime(23581): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 17:55:23.928: E/AndroidRuntime(23581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
03-24 17:55:23.928: E/AndroidRuntime(23581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
03-24 17:55:23.928: E/AndroidRuntime(23581): at dalvik.system.NativeStart.main(Native Method)
03-24 17:55:23.928: E/AndroidRuntime(23581): Caused by: java.lang.NullPointerException
03-24 17:55:23.928: E/AndroidRuntime(23581): at com.batz.voiceforyou.Things.onCreate(Things.java:19)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.Activity.performCreate(Activity.java:5013)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-24 17:55:23.928: E/AndroidRuntime(23581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
03-24 17:55:23.928: E/AndroidRuntime(23581): ... 18 more
这是 MainActivity 代码
public class MainActivity extends TabActivity {
String text="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText mainText=(EditText) findViewById(R.id.maintext);
Resources res = getResources();
TabHost tabs = getTabHost();
Intent regular = new Intent(this, Regular.class);
TabSpec reg = tabs.newTabSpec("Regular")
.setIndicator("", res.getDrawable(R.drawable.ic_launcher))
.setContent(regular);
Intent things = new Intent(this, Things.class);
TabSpec thngs = tabs.newTabSpec("Things")
.setIndicator("", res.getDrawable(R.drawable.ic_launcher))
.setContent(things);
tabs.addTab(reg);
tabs.addTab(thngs);
tabs.setCurrentTab(0);
}
}
这是在选项卡内加载的活动之一
public class Regular extends Activity {
String text="";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.regular);
//final EditText mainText=(EditText) findViewById(R.id.maintext);
//text=mainText.getText().toString();
Button regbtn = (Button) findViewById(R.id.regularbutton);
regbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
text=text+" regular";
//mainText.setText(text);
}
});
}
}
在这里您可以看到我评论的 3 行,如果我删除这些行,应用程序将在启动时强制关闭。如果你愿意,我也可以分享布局