RuntimeException:无法启动活动ComponentInfo:java.lang.NullPointerException致命异常

时间:2014-04-27 10:43:43

标签: java android xml

嗨,我是Android编程的新手,我遇到了这个问题,我确信我的代码应该可行,但不是。

这是我的MainActivity.java

 package com.example.evo;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageButton;

    public class MainActivity extends Activity {

    ImageButton imageButton2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open);



    imageButton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getApplicationContext(),  SecondScreenActivity.class);

        }
    } );
}
}

这是我的SeconScreenActirity.java

 import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageButton;


     public class SecondScreenActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.high_score);

    }



}

这是我的highscore.xml布局文件

 <?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:background="@drawable/yedi"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="220dp"
        android:text="high scores"
        android:textColor="#FA5882" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="29dp"
        android:text="Evrim"
        android:textColor="#00FFFF" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="-17dp"
        android:text="1528"
        android:textColor="#CC2EFA" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="20dp"
        android:text="Selin"
        android:textColor="#00FFFF" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="-17dp"
        android:text="1016"
        android:textColor="#CC2EFA" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="11dp"
        android:text="Muhammed"
        android:textColor="#00FFFF" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="-17dp"
        android:text="800"
        android:textColor="#CC2EFA" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="72dp"
        android:layout_height="0dp"
        android:layout_marginTop="80dp"
        android:layout_weight="0.16"
        android:src="@drawable/abc_ic_ab_back_holo_light"  
        android:contentDescription="@string/todo"/>

    </LinearLayout>

这是我的open.xml,我有我的imageButton2

<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:background="@drawable/son"
    android:paddingBottom="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_horizontal_margin"
    android:screenOrientation="landscape"
    tools:context="com.example.kkk.MainActivity$PlaceholderFragment" >

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/highscore" android:contentDescription="@string/todo"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_above="@+id/imageButton2"
        android:layout_alignLeft="@+id/imageButton2"
        android:src="@drawable/play" android:contentDescription="@string/todo"/>

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton3"
        android:layout_centerHorizontal="true"
        android:src="@drawable/exit" android:contentDescription="@string/todo"/>

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:src="@drawable/aboutt" android:contentDescription="@string/todo"/>

</RelativeLayout>

我还更新了我的AndroidManifest.xml以添加SeconScreenActivity.java 而且我得到了这个      java.lang.RuntimeException:无法启动活动      ComponentInfo java.lang.NullPointerException 错误请帮忙!

3 个答案:

答案 0 :(得分:5)

您永远不会在第一个活动中初始化您的imageButton元素。

因此,当您尝试为其分配点击侦听器时,您会获得NullPointerException

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.open);

     //or whatever the id is for the image button in the open layout
     imageButton2 = (ImageButton) findViewById(R.id.imageButton2); 
     imageButton2.setOnClickListener(new View.OnClickListener() {

答案 1 :(得分:0)

您错过了ImageButton布局中open的参考:

imageButton2 = (ImageButton) findViewById(R.id.<ImageButtonIdInOpenXml);

答案 2 :(得分:0)

只需添加以下行

imageButton2 = (ImageButton)findViewById(R.id.imageButton1);

<强>的setContentView(R.layout.open);

由于未初始化视图 ImageButton ,您收到错误