gpt4 book ai didi

android - 在操作栏中实现向上导航按钮时出错 : java. lang.RuntimeException:无法启动 Activity ComponentInfo

转载 作者:行者123 更新时间:2023-11-29 21:05:14 25 4
gpt4 key购买 nike

我正在处理在操作栏中实现导航按钮的项目,出现以下错误。我的项目包含两个java文件,两个xml文件

LOGCAT 错误:

07-17 14:19:02.277: D/gralloc_goldfish(2120): Emulator without GPU emulation detected.
07-17 14:19:43.937: I/Choreographer(2120): Skipped 113 frames! The application may be doing too much work on its main thread.
07-17 14:19:44.097: D/dalvikvm(2120): newInstance failed: Lcom/app/Nav; not accessible to Landroid/app/Instrumentation;
07-17 14:19:44.097: D/AndroidRuntime(2120): Shutting down VM
07-17 14:19:44.167: W/dalvikvm(2120): threadid=1: thread exiting with uncaught exception (group=0xb4ab6ba8)
07-17 14:19:44.347: E/AndroidRuntime(2120): FATAL EXCEPTION: main
07-17 14:19:44.347: E/AndroidRuntime(2120): Process: com.app, PID: 2120
07-17 14:19:44.347: E/AndroidRuntime(2120): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.Nav}: java.lang.IllegalAccessException: access to class not allowed
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.os.Handler.dispatchMessage(Handler.java:102)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.os.Looper.loop(Looper.java:136)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-17 14:19:44.347: E/AndroidRuntime(2120): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 14:19:44.347: E/AndroidRuntime(2120): at java.lang.reflect.Method.invoke(Method.java:515)
07-17 14:19:44.347: E/AndroidRuntime(2120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-17 14:19:44.347: E/AndroidRuntime(2120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-17 14:19:44.347: E/AndroidRuntime(2120): at dalvik.system.NativeStart.main(Native Method)
07-17 14:19:44.347: E/AndroidRuntime(2120): Caused by: java.lang.IllegalAccessException: access to class not allowed
07-17 14:19:44.347: E/AndroidRuntime(2120): at java.lang.Class.newInstanceImpl(Native Method)
07-17 14:19:44.347: E/AndroidRuntime(2120): at java.lang.Class.newInstance(Class.java:1208)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-17 14:19:44.347: E/AndroidRuntime(2120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-17 14:19:44.347: E/AndroidRuntime(2120): ... 11 more

1)MainActivity.java

package com.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.linear_layout);
Button b= (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg) {
startActivity(new Intent(MainActivity.this, com.app.Nav.class));

}
});

}

}

2)导航.java

package com.app;


import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;



class Nav extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.relative_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
System.out.println("context entered");
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
System.out.println("menu nav");
return true;
}
return super.onOptionsItemSelected(item);
}

3)AndroidManifest.xml

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

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

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

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

</activity>
<activity
android:name="com.app.Nav"
android:label="@string/up_icon"
android:parentActivityName="com.app.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.app.MainActivity" />


</activity>


</application>

</manifest>

最佳答案

Nav 类需要公开。否则只能从这个包中的其他类访问,而启动activity的Android框架类在另一个包中。这会导致异常。

关于android - 在操作栏中实现向上导航按钮时出错 : java. lang.RuntimeException:无法启动 Activity ComponentInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24811019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com