简体   繁体   English

由java.lang.NullPointerException和java.lang.RuntimeException引起

[英]Caused by java.lang.NullPointerException and java.lang.RuntimeException

I keep getting this error, I am not sure what's causing it. 我不断收到此错误,我不确定是什么原因引起的。 I declared and initialized the variables, but the error didn't disappear. 我声明并初始化了变量,但是错误没有消失。 I have been looking at the same error but I can't catch the mistake. 我一直在寻找相同的错误,但我找不到错误。

05-01 20:57:43.162  32675-32675/com.ammar.customlistview1.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40be21f8)
05-01 20:57:43.172  32675-32675/com.ammar.customlistview1.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ammar.customlistview1.app/com.ammar.customlistview1.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
            at android.app.ActivityThread.access$600(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1810)
            at com.ammar.customlistview1.app.MainActivity.<init>(MainActivity.java:16)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
            at android.app.ActivityThread.access$600(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
            at dalvik.system.NativeStart.main(Native Method)

Here is my class code: 这是我的课程代码:

package com.ammar.customlistview1.app;

/**
 * Created by Ammar on 5/1/2014.
 */
public class person {

    private String name = "Ammar";
    private int age = 21;
    private int picture = R.drawable.ic_launcher;

    public person(String name, int age, int picture) {
        this.name = name;
        this.age = age;
        this.picture = picture;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getPicture() {
        return picture;
    }

    public void setPicture(int picture) {
        this.picture = picture;
    }
}

Here is my main activity code: 这是我的主要活动代码:

package com.ammar.customlistview1.app;

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

import java.util.ArrayList;


public class MainActivity extends Activity {

    personadaptor adapter ;
    ArrayList <person> theperson = new ArrayList<person>();
    ListView lv = (ListView) findViewById(R.id.lvmain);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        person p1 = new person("Ammar", 21 , R.drawable.ic_launcher);
        person p2 = new person("Ali", 25 ,  R.drawable.ic_launcher);
        person p3 = new person("Saber", 23 ,  R.drawable.ic_launcher);

        theperson.add(p1);
        theperson.add(p2);
        theperson.add(p3);

        adapter = new personadaptor(theperson, this);
        lv.setAdapter(adapter);
    }


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

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

您必须先调用setContentView()才能调用findViewById() setContentView()

You cannot use Android UI methods such as findViewById() in the initializers of an Activity (or similar) Class, as these are only valid during and after the call to onCreate(). 您不能在Activity(或类似)类的初始化程序中使用Android UI方法,例如findViewById(),因为这些方法仅在调用onCreate()期间和之后才有效。

Move your initialization code to onCreate() 将您的初始化代码移动到onCreate()

(And when you do so, be sure to put the findViewById() after setContentView() as Matiash so correctly points out - otherwise you will only get a different null pointer exception) (并且,当您这样做时,请确保将findViewById()放在MContent的setContentView()之后,以便正确指出-否则,您只会得到一个不同的 null指针异常)

1.ListView lv; 1.ListView lv; Listview initialization shld be after Listview初始化将在之后

setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvmain); 

2.no need to initialize this name,age and picture in person just write 2.无需亲自写名字,年龄和图片来初始化

    private String name;
    private int age ;
    private int picture ;

3.in adapterclass u need layout 3.在adapterclass中你需要布局

adapter = new personadaptor(theperson, this);//where is your row layout

暂无
暂无

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

相关问题 java.lang.RuntimeException:java.lang.NullPointerException处理 - java.lang.RuntimeException: java.lang.NullPointerException PROCESSING 获取java.lang.RuntimeException:java.lang.NullPointerException - Getting java.lang.RuntimeException: java.lang.NullPointerException 诊断“ RuntimeException:无法启动活动,原因是:java.lang.NullPointerException” - Diagnose a “RuntimeException: Unable to start activity Caused by: java.lang.NullPointerException” 造成原因:java.lang.NullPointerException - Caused by: java.lang.NullPointerException Java.lang.runtimeException无法启动活动componentinfo java.lang.nullpointerexception - Java.lang.runtimeexception unable to start activity componentinfo java.lang.nullpointerexception java.lang.runtimeexception无法启动活动componentinfo java.lang.nullpointerexception android - java.lang.runtimeexception unable to start activity componentinfo java.lang.nullpointerexception android java.lang.RuntimeException: 无法启动活动 ComponentInfo{....}: java.lang.NullPointerException - java.lang.RuntimeException: Unable to start activity ComponentInfo{....}: java.lang.NullPointerException java.lang.RuntimeException:无法启动接收器* .mycustomReceiver java.lang.NullPointerException - java.lang.RuntimeException: Unable to start receiver *.mycustomReceiver java.lang.NullPointerException java.lang.RuntimeException:无法实例化活动ComponentInfo java.lang.NullPointerException - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo java.lang.NullPointerException java.lang.RuntimeException:将结果ResultInfo {...}传递给activity {... FacebookSignUp}失败:java.lang.NullPointerException: - java.lang.RuntimeException: Failure delivering result ResultInfo{…} to activity {…FacebookSignUp}: java.lang.NullPointerException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM