Android按钮onClick更改活动导致应用程序崩溃

时间:2015-12-26 17:50:18

标签: android android-studio

我尝试通过点击按钮将当前活动从主活动更改为另一个活动,但之后它无法正常工作。当我点击按钮时,它会崩溃应用程序,我不知道为什么会这样做。我还尝试在MainActivity.java上将布局更改为所需的布局,并且模拟器成功加载了活动,因此我认为在单击时调用的方法存在一些问题。

MainActivity.java

package com.example.user.partyminigames;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

/* AppCompatActivity*/
public class MainActivity extends Activity {
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); */

/*        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        }); */
    }

    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.button:
                intent.setClass(context, NumberGuessingActivity.class);
                startActivity(intent);
                break;
            }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

NumberGuessingActivity.java

package com.example.user.partyminigames;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by USER on 26/12/2015.
 */
public class NumberGuessingActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numberguessing);

    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.partyminigames" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NumberGuessingActivity">
            <intent-filter>
                <action android:name="number.guessing"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView android:text="@string/testing" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/asplode"
        android:id="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:autoText="false"
        android:textColor="@color/lose" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_numberGuessing"
        android:id="@+id/button"
        android:layout_marginTop="111dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="onClick" />
</RelativeLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<!--        <android.support.v7.widget.Toolbar android:id="@+id/toolbar" -->
<!--            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" -->
<!--            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> -->

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

<!--    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" -->
<!--        android:layout_width="wrap_content" android:layout_height="wrap_content" -->
<!--        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" -->
<!--        android:src="@android:drawable/ic_dialog_email" /> -->

</android.support.design.widget.CoordinatorLayout>

logcat的

12-27 01:25:49.436 1878-1878/? E/AndroidRuntime: FATAL EXCEPTION: main
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime: Process: com.example.user.partyminigames, PID: 1878
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3823)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4438)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18422)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3818)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4438) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18422) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.content.ComponentName.<init>(ComponentName.java:77)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.content.Intent.setClass(Intent.java:6360)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.example.user.partyminigames.MainActivity.onClick(MainActivity.java:39)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3818) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4438) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18422) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
12-27 01:25:49.456 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.456 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.456 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.466 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.466 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.466 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressStandard.ogg
12-27 01:25:49.486 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressSpacebar.ogg
12-27 01:25:49.486 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressDelete.ogg
12-27 01:25:49.496 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressReturn.ogg
12-27 01:25:49.496 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressInvalid.ogg

我试图按照本书教我在Android中编码的内容,但我仍然坚持改变活动。希望上面的代码/日志提供足够的信息。

编辑:它是一个空指针异常,但我无法在日志中找到告诉我的位置。感谢您通知我非常常见的空指针异常。

4 个答案:

答案 0 :(得分:2)

content上,未初始化上下文变量。

更改

fmt.Printf("%+v\n", content)

MainActivity.java

并删除

intent.setClass(context, NumberGuessingActivity.class);

答案 1 :(得分:0)

intent.setClass(this, NumberGuessingActivity.class);

答案 2 :(得分:0)

您有context null,即未在

初始化
intent.setClass(context, NumberGuessingActivity.class);  

使用

intent.setClass(this, NumberGuessingActivity.class);

答案 3 :(得分:0)

  1. 您尚未初始化上下文。所以要么在onCreate中这样做:context = this.getContext()或者更好的只是在你的onClick()中替换你的

    intent.setClass(context, NumberGuessingActivity.class);

  2. intent.setClass(this, NumberGuessingActivity.class);
    
    1. 由于您已在清单文件中为NUmberGuessing活动设置过滤器,因此如果您还在意图中设置过滤器会更好