神舟NO.10 2020-03-09 07:57 采纳率: 50%
浏览 1357
已结题

android中使用模拟器闪退问题

运行模拟器后,点击界面的任意控件都会弹出 很抱歉,”caihongpai“已停止运行
不知道这是为什么,求各位大神帮忙
logcat代码:

03-09 07:50:14.924 3687-3687/? I/art: Late-enabling -Xcheck:jni
03-09 07:50:15.063 3687-3687/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-09 07:50:15.087 3687-3687/com.example.caihongpai I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
03-09 07:50:15.087 3687-3687/com.example.caihongpai I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
03-09 07:50:15.134 3687-3687/com.example.caihongpai D/AndroidRuntime: Shutting down VM
03-09 07:50:15.134 3687-3687/com.example.caihongpai E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.caihongpai, PID: 3687
    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) 


下面的是.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条回答 默认 最新

  • dsxjinzhiqiang 2020-03-09 10:32
    关注

    错是无法启动一个mainactivity,看看是不是清单文件配置有问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体