直接从手机运行应用程序后,应用程序立即关闭

时间:2019-02-23 04:53:16

标签: java android

我几个月来一直在创建和测试一个应用。它的一部分是将应用程序从Android Studio运行到模拟器,并且一切正常。但是,当我尝试直接从连接的设备安装或运行该应用时,该应用崩溃了片刻,并且没有任何有关错误或任何消息的消息框。

这是logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{thrill.ma.mathrill/thrill.ma.mathrill.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3318)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3429)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:109)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7555)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at thrill.ma.mathrill.Home.onCreate(Home.java:37)
        at android.app.Activity.performCreate(Activity.java:7343)
        at android.app.Activity.performCreate(Activity.java:7333)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1219)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3429) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:109) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7555) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469) 

这是我的home.java。

public class Home extends AppCompatActivity implements View.OnClickListener {
    DatabaseHelper  db;
    Helpers h = new Helpers();
    Data data = new Data();
    Button btnLessons, btnPVP, btnProgress;
    String username = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        db = new DatabaseHelper(this);
        //data.data(db);
        //db.InsertUserTest("Test");
        h.fullscreen(getWindow().getDecorView());

        try{
            username = getIntent().getExtras().getString("Username");
        } catch(Exception e) {
            username = "";
        }

        btnLessons = findViewById(R.id.btnLessons);
        btnPVP = findViewById(R.id.btnPVP);
        btnProgress = findViewById(R.id.btnProgress);

        btnLessons.setOnClickListener(this);
        btnPVP.setOnClickListener(this);
        btnProgress.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        switch(v.getId()) {
            case R.id.btnLessons:
                if(username.equals("")) intent = new Intent(this, Login.class);
                else intent = new Intent(this, LessonsMain.class);
                break;
            case R.id.btnPVP:
                intent = new Intent(this, PVP.class);
                break;
            case R.id.btnProgress:
                intent = new Intent(this, Quiz.class);
                break;
        }

        intent.putExtra("Username", "" + username);
        startActivity(intent);
    }
}


I would really appreciate your help. Thank you.

这是首页布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="25"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="600dp"
                android:layout_height="120dp"
                app:srcCompat="@drawable/title" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="80"
            android:gravity="left|center"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="55dp"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/btnLessons"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:background="@drawable/button_lessons" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="55dp"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/btnPVP"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:background="@drawable/button_pvp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="55dp"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/btnProgress"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:background="@drawable/button_progress" />
                </LinearLayout>

            </LinearLayout>

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

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

                <LinearLayout
                    android:layout_width="100dp"
                    android:layout_height="match_parent"
                    android:gravity="bottom|right|center"
                    android:orientation="vertical"
                    android:paddingBottom="30dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="80dp"
                        android:gravity="center"
                        android:orientation="horizontal">

                        <Button
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:background="@drawable/button_developer" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="80dp"
                        android:gravity="center"
                        android:orientation="horizontal">

                        <Button
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:background="@drawable/button_settings" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

0 个答案:

没有答案