簡體   English   中英

不幸的是,我的Android應用錯誤已停止

[英]my android app error unfortunately app has stopped

我是Android新手。

我剛剛開始構建我的第一個Android應用程序,並且在過去兩天里一直在努力解決此錯誤。

沒有找到解決方案,我不得不來到這里。

錯誤:

不幸的是“應用名稱”已停止

這是主要的活動布局:

<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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="TextView" />

</RelativeLayout>

這是另一種布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

logcat錯誤

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.os.Looper.loop(Looper.java:137)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at dalvik.system.NativeStart.main(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): Caused by: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314):     at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:01:29.772: E/AndroidRuntime(1314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:01:29.772: E/AndroidRuntime(1314):     ... 11 more
03-25 12:01:35.622: I/Process(1314): Sending signal. PID: 1314 SIG: 9
03-25 12:11:56.522: D/AndroidRuntime(1378): Shutting down VM
03-25 12:11:56.522: W/dalvikvm(1378): threadid=1: thread exiting with uncaught exception (group=0xb3adbb90)
03-25 12:11:56.542: E/AndroidRuntime(1378): FATAL EXCEPTION: main
03-25 12:11:56.542: E/AndroidRuntime(1378): Process: com.example.listview, PID: 1378
03-25 12:11:56.542: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.os.Looper.loop(Looper.java:137)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at dalvik.system.NativeStart.main(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378):     at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:11:56.542: E/AndroidRuntime(1378):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:11:56.542: E/AndroidRuntime(1378):     ... 11 more

主要活動代碼

package com.example.listview;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lst = (ListView)findViewById(R.id.listView1);
        //ListView mainListView = null;
        String[] test = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                "Jupiter", "Saturn", "Uranus", "Neptune"}; 
         ArrayList<String> planetList = new ArrayList<String>();  
            planetList.addAll( Arrays.asList(test) ); 
            ArrayAdapter<String>  listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);  

            // Add more planets. If you passed a String[] instead of a List<String>   
            // into the ArrayAdapter constructor, you must not add more items.   
            // Otherwise an exception will occur.  
            listAdapter.add( "Ceres" );  
            listAdapter.add( "Pluto" );  
            listAdapter.add( "Haumea" );  
            listAdapter.add( "Makemake" );  
            listAdapter.add( "Eris" );  

            // Set the ArrayAdapter as the ListView's adapter.  
            lst.setAdapter( listAdapter ); 

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

您的列表視圖不包含在活動的布局中,因此當您調用它時,它將為您提供空指針異常。 您可以將listview放在主要活動的布局內。

<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=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="TextView" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>

</RelativeLayout>

第36行是:

lst.setAdapter( listAdapter ); 

表示lst可能為空。

ListView lst = (ListView)findViewById(R.id.listView1); //NULL

由於加載了main_activity布局,該布局沒有id=listView1ListView ,因此無法找到視圖。

可能需要加載包含它的另一個布局。

ListView lst = (ListView)findViewById(R.id.listView1); 返回null,因為您沒有在setContentView(R.layout.activity_main);使用正確的內容setContentView(R.layout.activity_main); 您有兩種選擇:

  • 使用include將布局放入另一個布局中:

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ... > <TextView ... /> <include android:layout="@layout\\my_layout_listview" /> </RelativeLayout> 
  • 或設置包含您的ListView的其他布局:

     setContentView(R.layout.my_layout_listview); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM