-1

我有下面列出的例外情况,我添加了代码,在创建这个问题之前,我用谷歌搜索了这个问题,但没有找到解决方案,我找不到问题或在其他帖子上有相同错误的任何帮助,所以我错过了什么?

模拟器 android 版本 2.2 api 级别 8

这是问题所在:

07-07 19:40:20.782: E/AndroidRuntime(588): FATAL EXCEPTION: main
07-07 19:40:20.782: E/AndroidRuntime(588): java.lang.IllegalStateException: Could not execute method of the activity
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.view.View$1.onClick(View.java:2072)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.view.View.performClick(View.java:2408)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.view.View$PerformClick.run(View.java:8816)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.os.Handler.handleCallback(Handler.java:587)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.os.Looper.loop(Looper.java:123)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-07 19:40:20.782: E/AndroidRuntime(588):  at java.lang.reflect.Method.invokeNative(Native Method)
07-07 19:40:20.782: E/AndroidRuntime(588):  at java.lang.reflect.Method.invoke(Method.java:521)
07-07 19:40:20.782: E/AndroidRuntime(588):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-07 19:40:20.782: E/AndroidRuntime(588):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-07 19:40:20.782: E/AndroidRuntime(588):  at dalvik.system.NativeStart.main(Native Method)
07-07 19:40:20.782: E/AndroidRuntime(588): Caused by: java.lang.reflect.InvocationTargetException
07-07 19:40:20.782: E/AndroidRuntime(588):  at app.hearts.multiplayer.MainActivity.onPlayClick(MainActivity.java:24)
07-07 19:40:20.782: E/AndroidRuntime(588):  at java.lang.reflect.Method.invokeNative(Native Method)
07-07 19:40:20.782: E/AndroidRuntime(588):  at java.lang.reflect.Method.invoke(Method.java:521)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.view.View$1.onClick(View.java:2067)
07-07 19:40:20.782: E/AndroidRuntime(588):  ... 11 more
07-07 19:40:20.782: E/AndroidRuntime(588): Caused by: java.lang.SecurityException: Permission Denial: starting Intent { cmp=app.hearts.multiplayer/.GameActivity } from ProcessRecord{44fc09f0 588:app.hearts.multiplayer/10032} (pid=588, uid=10032) requires android.permission.ACCESS_NETWORK_STATE
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.os.Parcel.readException(Parcel.java:1247)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.os.Parcel.readException(Parcel.java:1235)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1298)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.app.Activity.startActivityForResult(Activity.java:2817)
07-07 19:40:20.782: E/AndroidRuntime(588):  at android.app.Activity.startActivity(Activity.java:2923)
07-07 19:40:20.782: E/AndroidRuntime(588):  ... 15 more

MainActivity.java:

package app.hearts.multiplayer;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;    
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}



public void onPlayClick(View view)
{
    Intent gameIntent = new Intent(MainActivity.this, GameActivity.class);
    startActivity(gameIntent);
}
}

活动主.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:gravity="right"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.05"
        android:scaleType="fitXY"
        android:src="@drawable/hearts_hat" />

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.70"
        android:orientation="vertical"
        android:paddingRight="@dimen/padding_small"
        android:paddingTop="@dimen/padding_large" >


        <ImageButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_weight="0.00"
            android:adjustViewBounds="true"
            android:background="@drawable/select_play"
            android:onClick="onPlayClick"
            android:clickable="true"
            android:paddingTop="@dimen/padding_medium" />

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/padding_large"
            android:paddingTop="@dimen/padding_large" >
        </TableRow>

        <ImageButton
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_weight="0.00"
            android:background="@drawable/select_highscore"
            android:clickable="true"
            android:paddingTop="@dimen/padding_large" />
    </LinearLayout>

</LinearLayout>

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.hearts.multiplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/hearts_icon"
        android:label="@string/app_name"
        android:permission="android.permission.ACCESS_NETWORK_STATE"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GameActivity"
            android:label="@string/title_activity_game" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

游戏活动.java:

package app.hearts.multiplayer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class GameActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
    }

}

活动游戏.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".GameActivity" />

</RelativeLayout>
4

1 回答 1

0

从下面的错误:

07-07 19:40:20.782: E/AndroidRuntime(588): Caused by: java.lang.SecurityException: Permission Denial: starting Intent { cmp=app.hearts.multiplayer/.GameActivity } from ProcessRecord{44fc09f0 588:app.hearts.multiplayer/10032} (pid=588, uid=10032) requires android.permission.ACCESS_NETWORK_STATE

您似乎需要添加ACCESS_NETWORK_STATE - 使用以下代码添加此权限,该代码应位于“应用程序”标签之外:

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!-- more data goes here... -->
    </application>  

</manifest>

试试看。

于 2012-07-07T20:03:01.147 回答