有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓运行时异常:无法启动活动组件信息:java。lang.NullPointerException致命异常

嗨,我是安卓编程新手,我遇到了这个问题,我确信我的代码应该可以工作,但不能

这是我的MainActivity.java

 package com.example.evo;

    import 安卓.app.Activity;
    import 安卓.content.Intent;
    import 安卓.os.Bundle;
    import 安卓.util.Log;
    import 安卓.view.View;
    import 安卓.view.View.OnClickListener;
    import 安卓.widget.Button;
    import 安卓.widget.EditText;
    import 安卓.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);

        }
    } );
}
}

这是我的第二信条。爪哇

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


     public class SecondScreenActivity extends Activity {


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

    }



}

这是我的高分。xml布局文件

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="@drawable/yedi"
    安卓:orientation="vertical" >

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

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

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

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

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

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

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

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

    </LinearLayout>

这是我的公开课。xml在哪里有我的imageButton2

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="@drawable/son"
    安卓:paddingBottom="@dimen/activity_horizontal_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_horizontal_margin"
    安卓:screenOrientation="landscape"
    tools:context="com.example.kkk.MainActivity$PlaceholderFragment" >

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

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

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

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

</RelativeLayout>

我还更新了我的AndroidManifest。添加SeconScreenActivity的xml。JAVA 我明白了 JAVAlang.RuntimeException:无法启动活动 ComponentInfoJava。lang.NullPointerException 错误请大家帮忙


共 (3) 个答案

  1. # 1 楼答案

    您从未在第一个活动中初始化imageButton元素

    因此,当您尝试为其分配click侦听器时,会得到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() {
    
  2. # 2 楼答案

    您没有从open布局中引用ImageButton

    imageButton2 = (ImageButton) findViewById(R.id.<ImageButtonIdInOpenXml);
    
  3. # 3 楼答案

    只需在下面一行加上

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

    之后

    setContentView(R.layout.open)

    由于视图ImageButton未初始化,因此出现错误