您好我已将我的sdk更新为api级别21,从那时起我收到很多错误。我的简单应用程序在开始给我运行时异常之前没有给出任何类型的错误。 这是我的mainactivity.java类:
package com.example.myfirstapp;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem shareItem=menu.findItem(R.id.action_share);
ShareActionProvider m=(ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
m.setShareIntent(getDefaultIntent());
return true;
}
private Intent getDefaultIntent() {
// TODO Auto-generated method stub
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
return intent;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</application>
我的main.xml:
<menu 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"
tools:context="com.example.myfirstapp.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom|withText"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="@+id/action_share"
android:icon="@drawable/ic_action_share"
android:orderInCategory="100"
android:title="@string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
</menu>
当我运行这个简单的应用程序时,我收到以下错误:
10-29 03:08:48.153: E/AndroidRuntime(1481): FATAL EXCEPTION: main
10-29 03:08:48.153: E/AndroidRuntime(1481): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
com.example.myfirstapp.com.example.myfirstapp.MainActivity
10-29 03:08:48.153: E/AndroidRuntime(1481):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.os.Looper.loop(Looper.java:137)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-29 03:08:48.153: E/AndroidRuntime(1481): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 03:08:48.153: E/AndroidRuntime(1481): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 03:08:48.153: E/AndroidRuntime(1481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-29 03:08:48.153: E/AndroidRuntime(1481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 03:08:48.153: E/AndroidRuntime(1481): at dalvik.system.NativeStart.main(Native Method)
10-29 03:08:48.153: E/AndroidRuntime(1481): Caused by: java.lang.ClassNotFoundException: com.example.myfirstapp.com.example.myfirstapp.MainActivity
10-29 03:08:48.153: E/AndroidRuntime(1481): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
10-29 03:08:48.153: E/AndroidRuntime(1481): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-29 03:08:48.153: E/AndroidRuntime(1481): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-29 03:08:48.153: E/AndroidRuntime(1481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-29 03:08:48.153: E/AndroidRuntime(1481): ... 11 more
答案 0 :(得分:0)
我可以看到您的课程为com.example.myfirstapp.MainActivity
,但程序无法找到com.example.myfirstapp.com.example.myfirstapp.MainActivity
,因为它已将您的包名称加倍。
AndroidManifest.xml
中的尝试将主类与包名称而不是.MainActivity
放在一起。那会有用吗?
<application>
<activity
android:name="com.example.myfirstapp.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>