我有一个名为Information的菜单按钮,当我触摸它时,我想要一个名为" InformationActivity"推出。 我已经验证了代码,但似乎没有任何效果。我得到一个NullPointer异常和一个FATAL EXCEPTION:Main。 为什么会这样?查看代码以查看失败的位置。谢谢。
这里有与菜单相关的所有内容;
@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(1,INFORMATION,1,"Information");
Log.w("I AM HERE ?!", "YES01");
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case INFORMATION:
Log.w("I AM HERE ?!", "YES02");
Toast.makeText(getApplicationContext(), "This works - tested.", Toast.LENGTH_LONG).show();
Intent infoIntent = new Intent(this,InformationActivity.class);
Log.w("I AM HERE ?!", infoIntent.toString());
//此日志显示为"包名称/ .InformationActivity" -为什么?!这是正确的格式吗? startActivity(infoIntent); //在这里失败 return true;
default:
return super.onOptionsItemSelected(item);
}
这是AndroidManifest.xml中的相关代码;
<activity
android:name="com.example.pingtest.InformationActivity"
android:label="@string/title_activity_information" >
</activity>
这里是activity_information.xml文件。请注意,我在InformationActivity类的Java代码中使用线性布局。但是,它并没有出现在这里。当我使用Java在那里添加新的TextView时,我不需要它。希望有道理。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".InformationActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
这里是Logcat输出
11-29 16:55:04.223: I/ViewRootImpl(2740): ViewRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_MENU, scanCode=139, metaState=0, flags=0x48, repeatCount=0, eventTime=60889321, downTime=60889321, deviceId=7, source=0x101 } to com.android.internal.policy.impl.PhoneWindow$DecorView{4286a2b8 V.E..... R....... 0,0-1080,1920}
11-29 16:55:04.293: I/ViewRootImpl(2740): ViewRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MENU, scanCode=139, metaState=0, flags=0x48, repeatCount=0, eventTime=60889391, downTime=60889321, deviceId=7, source=0x101 } to com.android.internal.policy.impl.PhoneWindow$DecorView{4286a2b8 V.E..... R....... 0,0-1080,1920}
11-29 16:55:04.873: I/ViewRootImpl(2740): ViewRoot's Touch Event : Touch Down
11-29 16:55:04.933: I/ViewRootImpl(2740): ViewRoot's Touch Event : Touch UP
11-29 16:55:05.003: W/I AM HERE ?!(2740): YES02
11-29 16:55:05.023: W/I AM HERE ?!(2740): Intent { cmp=com.example.pingtest/.InformationActivity }
11-29 16:55:05.023: I/ActivityManager(2740): Timeline: Activity_launch_request id:com.example.pingtest time:60890123
11-29 16:55:05.093: D/AndroidRuntime(2740): Shutting down VM
11-29 16:55:05.093: W/dalvikvm(2740): threadid=1: thread exiting with uncaught exception (group=0x41822e48)
11-29 16:55:05.103: E/AndroidRuntime(2740): FATAL EXCEPTION: main
11-29 16:55:05.103: E/AndroidRuntime(2740): Process: com.example.pingtest, PID: 2740
11-29 16:55:05.103: E/AndroidRuntime(2740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pingtest/com.example.pingtest.InformationActivity}: java.lang.NullPointerException
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.os.Looper.loop(Looper.java:136)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-29 16:55:05.103: E/AndroidRuntime(2740): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 16:55:05.103: E/AndroidRuntime(2740): at java.lang.reflect.Method.invoke(Method.java:515)
11-29 16:55:05.103: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-29 16:55:05.103: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
11-29 16:55:05.103: E/AndroidRuntime(2740): at dalvik.system.NativeStart.main(Native Method)
11-29 16:55:05.103: E/AndroidRuntime(2740): Caused by: java.lang.NullPointerException
11-29 16:55:05.103: E/AndroidRuntime(2740): at com.example.pingtest.InformationActivity.onCreate(InformationActivity.java:24)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.Activity.performCreate(Activity.java:5275)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
11-29 16:55:05.103: E/AndroidRuntime(2740): ... 11 more
这是InformationActivity.java文件。我在这里看不到任何错误。
public class InformationActivity extends Activity {
LinearLayout layout2;
TextView label2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//readTask = new ReadTask();
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/41370_WINDSORCONDENSED.TTF");
layout2=new LinearLayout(this);
label2.setText("Text here\n");
label2.setTextColor(Color.rgb(0,0,0));
label2.setBackgroundColor(Color.rgb(255,194,75));
label2.setTypeface(type);
setContentView(R.layout.activity_information);
}
答案 0 :(得分:1)
label2.setText
的引用之前,会调用 label2
。首先,通过调用setContentView(R.layout.activity_information);
来设置活动布局。然后使用findViewById
获取对相应TextView的引用。
编辑:完整修复:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
//readTask = new ReadTask();
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/41370_WINDSORCONDENSED.TTF");
label2 = (TextView) findViewById(R.id.label_2_id_here);
label2.setText("Text here\n");
label2.setTextColor(Color.rgb(0,0,0));
label2.setBackgroundColor(Color.rgb(255,194,75));
label2.setTypeface(type);
}