简体   繁体   English

不幸的是,我的Android应用错误已停止

[英]my android app error unfortunately app has stopped

I am new to Android. 我是Android新手。

I've just started to build my first Android app and have been struggling with this errorfor the past 2 days. 我刚刚开始构建我的第一个Android应用程序,并且在过去两天里一直在努力解决此错误。

Finding no solution I had to come here. 没有找到解决方案,我不得不来到这里。

Error: 错误:

Unfortunately "app name" has stopped 不幸的是“应用名称”已停止

This is main activity layout: 这是主要的活动布局:

<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>

This is another layout: 这是另一种布局:

<?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 error 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

main activity code 主要活动代码

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;
    }

}

Your listview is not contained in the activity's layout so when you call it it gives you a null pointer exception. 您的列表视图不包含在活动的布局中,因此当您调用它时,它将为您提供空指针异常。 you could put the listview inside the main activity's layout. 您可以将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>

Line 36 is: 第36行是:

lst.setAdapter( listAdapter ); 

Means that lst is probably null. 表示lst可能为空。

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

Failed to find the view, because you load main_activity layout, which does not have a ListView with id=listView1 . 由于加载了main_activity布局,该布局没有id=listView1ListView ,因此无法找到视图。

Probably you need to load another layout, which it contains. 可能需要加载包含它的另一个布局。

ListView lst = (ListView)findViewById(R.id.listView1); return null because you don't use the right content in setContentView(R.layout.activity_main); 返回null,因为您没有在setContentView(R.layout.activity_main);使用正确的内容setContentView(R.layout.activity_main); . You have two options: 您有两种选择:

  • Use a include to put a layout inside another: 使用include将布局放入另一个布局中:

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ... > <TextView ... /> <include android:layout="@layout\\my_layout_listview" /> </RelativeLayout> 
  • Or set the other layout which contains your ListView in: 或设置包含您的ListView的其他布局:

     setContentView(R.layout.my_layout_listview); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM