gpt4 book ai didi

java - RuntimeException : Unable to start activity ComponentInfo: java. lang.NullPointerException 致命异常

转载 作者:行者123 更新时间:2023-12-01 17:11:58 27 4
gpt4 key购买 nike

嗨,我是 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:无法启动 Activity ComponentInfo java.lang.NullPointerException错误请帮忙!

最佳答案

您永远不会在第一个 Activity 中初始化 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() {

关于java - RuntimeException : Unable to start activity ComponentInfo: java. lang.NullPointerException 致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com