Java 实例化新活动会使应用程序崩溃
我是Android应用程序开发新手,现在正尝试编写第一个应用程序。位当我想显示另一个活动时,应用程序会崩溃,并出现以下日志(取自LogCat): 执行的代码是:Java 实例化新活动会使应用程序崩溃,java,android,android-intent,Java,Android,Android Intent,我是Android应用程序开发新手,现在正尝试编写第一个应用程序。位当我想显示另一个活动时,应用程序会崩溃,并出现以下日志(取自LogCat): 执行的代码是: //HomeScreen.java //... public void gotoNews (View view) { Log.d(TAG, "news_button clicked"); Intent news = new Intent(this, NewsScreen.class); startActivity
//HomeScreen.java
//...
public void gotoNews (View view) {
Log.d(TAG, "news_button clicked");
Intent news = new Intent(this, NewsScreen.class);
startActivity(news);
}
//NewsScreen.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_screen);
Log.d("NewsScreen", "created");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.update_text).setTitle(R.string.update_title).create().show();
Log.d("NewsScreen", "Started update");
}
我在代码中找不到错误,因为日志中没有行号。
请帮助我。您是否已将此新闻屏幕活动添加到清单文件中?这是崩溃活动最常见的问题。您需要将活动添加到要重定向到的清单文件中 前
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.the_Seppi.freakhall"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="me.the_Seppi.freakhall.HomeScreen"
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="me.the_Seppi.freakhall.NewsScreen"
android:label="@string/title_activity_news_screen" >
</activity>
</application>
</manifest>
我希望这将帮助您解决此问题。问题已解决:只需重新创建活动新闻屏幕及其所有资源
但是代码和清单条目是相同的。=>出了什么问题?请发布您的清单和更多的
newscreen.java
我必须等待8小时才能回答我自己的问题…除非有一条我还不知道的新规则,你现在应该可以写一个答案了,但是你一天左右都不能接受。我想当我使用这个向导时,eclipse插件会自动完成这个任务。是的,它是自动添加的。正如你在我之前的评论中看到的,eclipse是自动添加的。我没有更改清单中的任何内容,它是100%由向导生成的
<activity android:name="sanatan.engine.MainActivity" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.the_Seppi.freakhall"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="me.the_Seppi.freakhall.HomeScreen"
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="me.the_Seppi.freakhall.NewsScreen"
android:label="@string/title_activity_news_screen" >
</activity>
</application>
</manifest>
public abstract class NewsScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_screen);
Log.d("NewsScreen", "created");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.update_text).setTitle(R.string.update_title).create().show();
Log.d("NewsScreen", "Started update");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_news_screen, menu);
return true;
}
}