android初学者的一些小问题 5C

在模拟器上运行程序时点击任意位置会出现 很抱歉,“My applicationz"已停止运行
找了半天也不知道是为什么,下面的是logcat代码

03-08 22:53:34.063 5761-5761/? I/art: Late-enabling -Xcheck:jni
03-08 22:53:34.184 5761-5761/com.example.caihongpai W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-08 22:53:34.210 5761-5761/com.example.caihongpai I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
03-08 22:53:34.210 5761-5761/com.example.caihongpai I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
03-08 22:53:34.259 5761-5761/com.example.caihongpai D/AndroidRuntime: Shutting down VM
03-08 22:53:34.259 5761-5761/com.example.caihongpai E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.caihongpai, PID: 5761
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.caihongpai/com.example.caihongpai.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
        at android.app.ActivityThread.access$800(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5305)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.caihongpai.MainActivity.onCreate(MainActivity.java:121)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
        at android.app.ActivityThread.access$800(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5305) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
03-08 22:58:34.283 5761-5761/com.example.caihongpai I/Process: Sending signal. PID: 5761 SIG: 9

下面的是.java代码

package com.example.caihongpai;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button b1;
    ImageButton ib1,ib2,ib3;
    ImageView i1,i2,i3;
    TextView t1;
    static  int[] s1={R.drawable.fangpian,R.drawable.heitao,R.drawable.hongtao};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button)findViewById(R.id.button);
        ib1=(ImageButton) findViewById(R.id.ib1);
        ib2=(ImageButton) findViewById(R.id.ib2);
        ib2=(ImageButton) findViewById(R.id.ib2);
        i1=(ImageView) findViewById(R.id.i1);
        i2=(ImageView) findViewById(R.id.i2);
        i3=(ImageView) findViewById(R.id.i3);

        ib1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ib1.setImageResource(s1[0]);
                ib2.setImageResource(s1[1]);
                ib3.setImageResource(s1[2]);

                if(s1[0]==R.drawable.hongtao){
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("奥利给")
                            .setMessage("小伙子,你答对了,真是个天才!!")
                            .setPositiveButton("确认",null)
                            .show();
                    i3.setVisibility(View.VISIBLE);
                    ib1.setVisibility(View.INVISIBLE);
                    ib2.setVisibility(View.INVISIBLE);
                    ib3.setVisibility(View.INVISIBLE);
                }else{
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("向我开炮")
                            .setMessage("小傻瓜,你答错了,真是个小笨蛋!!")
                            .setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialogInterface, int which) {
                                    return;
                                }
                            })
                            .setNegativeButton("再试一次", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    xp();
                                }
                            })
                            .show();
                    i3.setVisibility(View.VISIBLE);
                    ib1.setVisibility(View.INVISIBLE);
                    ib2.setVisibility(View.INVISIBLE);
                    ib3.setVisibility(View.INVISIBLE);
                }
            }
        });



        ib2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ib1.setImageResource(s1[0]);
                ib2.setImageResource(s1[1]);
                ib3.setImageResource(s1[2]);

                if(s1[1]==R.drawable.hongtao){
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("奥利给")
                            .setMessage("小伙子,你答对了,真是个天才!!")
                            .setPositiveButton("确认",null)
                            .show();
                    i3.setVisibility(View.VISIBLE);
                    ib1.setVisibility(View.INVISIBLE);
                    ib2.setVisibility(View.INVISIBLE);
                    ib3.setVisibility(View.INVISIBLE);
                }else{
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("向我开炮")
                            .setMessage("小傻瓜,你答错了,真是个小笨蛋!!")
                            .setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialogInterface, int which) {
                                    return;
                                }
                            })
                            .setNegativeButton("再试一次", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    xp();
                                }
                            })
                            .show();
                    i3.setVisibility(View.VISIBLE);
                    ib1.setVisibility(View.INVISIBLE);
                    ib2.setVisibility(View.INVISIBLE);
                    ib3.setVisibility(View.INVISIBLE);
                }
            }
        });


        ib3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ib1.setImageResource(s1[0]);
                ib2.setImageResource(s1[1]);
                ib3.setImageResource(s1[2]);

                if(s1[2]==R.drawable.hongtao){
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("奥利给")
                            .setMessage("小伙子,你答对了,真是个天才!!")
                            .setPositiveButton("确认",null)
                            .show();
                    i3.setVisibility(View.VISIBLE);
                    ib1.setVisibility(View.INVISIBLE);
                    ib2.setVisibility(View.INVISIBLE);
                    ib3.setVisibility(View.INVISIBLE);
                }else{
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("向我开炮")
                            .setMessage("小傻瓜,你答错了,真是个小笨蛋!!")
                            .setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialogInterface, int which) {
                                    return;
                                }
                            })
                            .setNegativeButton("再试一次", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    xp();
                                }
                            })
                            .show();
                    i3.setVisibility(View.VISIBLE);
                    ib1.setVisibility(View.INVISIBLE);
                    ib2.setVisibility(View.INVISIBLE);
                    ib3.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
    private void xp() {
        for (int i = 0; i < 3; i++) {
            int tmp = s1[i];
            int s = (int) (Math.random() * 2);
            s1[i] = s1[s];
            s1[s] = tmp;
        }
    }
}

下面的是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#0CC6BB"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="猜猜红桃A在哪"
        android:textSize="30dp"
        android:gravity="center"
        android:layout_marginTop="50dp"
        />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_margin="10dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/ib1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:foregroundGravity="top"
                app:srcCompat="@drawable/beimian" />

            <ImageButton
                android:id="@+id/ib2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:foregroundGravity="top"
                app:srcCompat="@drawable/beimian" />

            <ImageButton
                android:id="@+id/ib3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:foregroundGravity="top"
                app:srcCompat="@drawable/beimian" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/i1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/heitao"
                android:visibility="invisible"/>

            <ImageView
                android:id="@+id/i2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/fangpian"
                android:visibility="invisible"/>

            <ImageView
                android:id="@+id/i3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/hongtao"
                android:visibility="invisible"
                />
        </LinearLayout>

    </FrameLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="再玩一次噻"
        android:textSize="20dp"
        />

</LinearLayout>

希望各位大神帮帮忙

2个回答

at com.example.myapplicationz.MainActivity$2.onClick(MainActivity.java:48)
看mainactivity的onclick的代码,里面丢出了null

qq_43266835
神舟NO.10 不好意思我没太懂,具体怎么操作才能解决这个问题?
4 个月之前 回复
qq_43266835
神舟NO.10 不好意思我没太懂,具体怎么操作才能解决这个问题?
4 个月之前 回复

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.caihongpai.MainActivity.onCreate(MainActivity.java:121)

log中已经说明白原因了,也提示了具体的代码行数在121行,设置监听器的ImageButton 为null

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问