我正在做Android開發教程我的第一個應用程序。在執行操作欄類後,我在模擬器上運行應用程序時遇到問題。我的任何文件(java或xml)都沒有錯誤。然而,我的logcat中的第一條紅線是「04-03 15:49:37.597:E/AndroidRuntime(1115):FATAL EXCEPTION:main」。我研究了以前關於致命異常的所有文章,但還沒有找到我的答案。請幫幫我。我的主要活動的java文件是:04-03 15:49:37.597:E/AndroidRuntime(1115):致命異常:主要
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>
我的a ctivity_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已停止」
您有來自第20行的NullPointerException。哪一個是? – thegrinner
04-03 15:49:37.597:E/AndroidRuntime(1115):引起:java.lang.NullPointerException – user3495505
第20行代碼,而不是堆棧跟蹤。如果你從logcat的NPE中讀出來,你會看到第10行是'at com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20) 04-03 15:49:37.597:E/AndroidRuntime 1115):在android.app.Activity.performCreate(Activity.java:5231)'。 – thegrinner