我在做Android开发教程My First App。在执行操作栏类之后,我在模拟器上运行应用程序时遇到问题。我的任何文件(java或xml)都没有错误。然而,我的logcat中的第一个红线是“04-03 15:49:37.597:E / AndroidRuntime(1115):FATAL EXCEPTION:main”。我已经研究过所有关于致命异常的帖子,但还没有找到答案。请帮我。我的主要活动java文件是:
package com.example.myfirstapp2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp2.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your miniSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_search) {
openSearch();
return true;
} else if (itemId == R.id.action_settings) {
openSettings();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
private void openSettings() {
// TODO Auto-generated method stub
}
private void openSearch() {
// TODO Auto-generated method stub
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
/**Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
我的主要活动xml是:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myfirstapp2.MainActivity"
tools:ignore="MergeRootFrame" />
我的显示消息活动Java文件是:
package com.example.myfirstapp2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
我的fragment_display_message xml是:
<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="com.example.myfirstapp2.DisplayMessageActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
我的activity_display_message xml是:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myfirstapp2.DisplayMessageActivity"
tools:ignore="MergeRootFrame" />
我的fragment_main xml是:
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
我的logcat是:
04-03 15:49:37.577: D/AndroidRuntime(1115): Shutting down VM
04-03 15:49:37.577: W/dalvikvm(1115): threadid=1: thread exiting with uncaught exception (group=0xb1abdba8)
04-03 15:49:37.597: E/AndroidRuntime(1115): FATAL EXCEPTION: main
04-03 15:49:37.597: E/AndroidRuntime(1115): Process: com.example.myfirstapp2, PID: 1115
04-03 15:49:37.597: E/AndroidRuntime(1115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp2/com.example.myfirstapp2.MainActivity}: java.lang.NullPointerException
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.os.Looper.loop(Looper.java:136)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 15:49:37.597: E/AndroidRuntime(1115): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:49:37.597: E/AndroidRuntime(1115): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 15:49:37.597: E/AndroidRuntime(1115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 15:49:37.597: E/AndroidRuntime(1115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 15:49:37.597: E/AndroidRuntime(1115): at dalvik.system.NativeStart.main(Native Method)
04-03 15:49:37.597: E/AndroidRuntime(1115): Caused by: java.lang.NullPointerException
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.view.ViewGroup.addViewInner(ViewGroup.java:3561)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.view.ViewGroup.addView(ViewGroup.java:3391)
04-03 15:49:37.597: E/AndroidRuntime(1115): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
04-03 15:49:37.597: E/AndroidRuntime(1115): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.Activity.setContentView(Activity.java:1949)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:220)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.jav a:106)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:81)
04-03 15:49:37.597: E/AndroidRuntime(1115): at com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.Activity.performCreate(Activity.java:5231)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 15:49:37.597: E/AndroidRuntime(1115): ... 11 more
04-03 15:49:42.597: I/Process(1115): Sending signal. PID: 1115 SIG: 9
04-03 15:50:36.957: D/AndroidRuntime(1135): Shutting down VM
04-03 15:50:36.957: W/dalvikvm(1135): threadid=1: thread exiting with uncaught exception (group=0xb1abdba8)
04-03 15:50:36.977: E/AndroidRuntime(1135): FATAL EXCEPTION: main
04-03 15:50:36.977: E/AndroidRuntime(1135): Process: com.example.myfirstapp2, PID: 1135
04-03 15:50:36.977: E/AndroidRuntime(1135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp2/com.example.myfirstapp2.MainActivity}: java.lang.NullPointerException
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.os.Looper.loop(Looper.java:136)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 15:50:36.977: E/AndroidRuntime(1135): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:50:36.977: E/AndroidRuntime(1135): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 15:50:36.977: E/AndroidRuntime(1135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 15:50:36.977: E/AndroidRuntime(1135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 15:50:36.977: E/AndroidRuntime(1135): at dalvik.system.NativeStart.main(Native Method)
04-03 15:50:36.977: E/AndroidRuntime(1135): Caused by: java.lang.NullPointerException
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.view.ViewGroup.addViewInner(ViewGroup.java:3561)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.view.ViewGroup.addView(ViewGroup.java:3391)
04-03 15:50:36.977: E/AndroidRuntime(1135): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
04-03 15:50:36.977: E/AndroidRuntime(1135): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.Activity.setContentView(Activity.java:1949)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:220)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.jav a:106)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:81)
04-03 15:50:36.977: E/AndroidRuntime(1135): at com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.Activity.performCreate(Activity.java:5231)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 15:50:36.977: E/AndroidRuntime(1135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 15:50:36.977: E/AndroidRuntime(1135): ... 11 more
04-03 15:50:41.527: I/Process(1135): Sending signal. PID: 1135 SIG: 9
04-03 15:54:55.657: D/AndroidRuntime(1174): Shutting down VM
04-03 15:54:55.657: W/dalvikvm(1174): threadid=1: thread exiting with uncaught exception (group=0xb1abdba8)
04-03 15:54:55.687: E/AndroidRuntime(1174): FATAL EXCEPTION: main
04-03 15:54:55.687: E/AndroidRuntime(1174): Process: com.example.myfirstapp2, PID: 1174
04-03 15:54:55.687: E/AndroidRuntime(1174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp2/com.example.myfirstapp2.MainActivity}: java.lang.NullPointerException
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 15:54:55.687: E/AndroidRuntime(1174): at 04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.os.Looper.loop(Looper.java:136)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 15:54:55.687: E/AndroidRuntime(1174): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:54:55.687: E/AndroidRuntime(1174): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 15:54:55.687: E/AndroidRuntime(1174): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 15:54:55.687: E/AndroidRuntime(1174): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 15:54:55.687: E/AndroidRuntime(1174): at dalvik.system.NativeStart.main(Native Method)
04-03 15:54:55.687: E/AndroidRuntime(1174): Caused by: java.lang.NullPointerException
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.view.ViewGroup.addViewInner(ViewGroup.java:3561)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.view.ViewGroup.addView(ViewGroup.java:3391)
04-03 15:54:55.687: E/AndroidRuntime(1174): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
04-03 15:54:55.687: E/AndroidRuntime(1174): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.Activity.setContentView(Activity.java:1949)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:220 )
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDel egateICS.java:106)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:81)
04-03 15:54:55.687: E/AndroidRuntime(1174): at com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.Activity.performCreate(Activity.java:5231)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 15:54:55.687: E/AndroidRuntime(1174): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 15:54:55.687: E/AndroidRuntime(1174): ... 11 more
04-03 15:55:06.527: I/Process(1174): Sending signal. PID: 1174 SIG: 9
我该怎么办?当我在模拟器上运行它时,我收到一条消息“不幸的是MyFirstApp已停止”
答案 0 :(得分:2)
activity_display_message
是XML文件的名称和
setContentView(R.layout.activity_displaymessage);
所以&#34;显示&#34;之间没有下划线。和&#34;消息&#34;