我正在嘗試學習android/java編程的基礎知識。我從教程中創建了一個基本的RSS android應用程序。我添加了另一個活動來創建一個新的開始屏幕,我試圖鏈接到教程中的主要RSS活動,但是當我運行該應用程序時,當我點擊新開始屏幕上的按鈕時,它總是崩潰。我在這裏做了明顯錯誤的事嗎?Android:連接兩個活動並相應地更改清單
這裏的XML「firstscreen」爲我創造
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="gotorssfunction"
android:text="@string/rssfeed" />
</LinearLayout>
在這裏,新的開始屏幕是我創建的是新的開始屏幕
package com.testproject.newsapp2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainScreen extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreen);
}
public void gotorssfuction(View v){
Intent i = new Intent(this, NewsApp2Activity.class);
startActivity(i);
}
}
在這裏,新的活動是一個從主要的RSS活動教程(它曾經是開始屏幕)
package com.testproject.newsapp2;
import java.util.List;
import com.testproject.newsapp2.data.RssItem;
import com.testproject.newsapp2.listeners.ListListener;
import com.testproject.newsapp2.util.RssReader;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Main application activity.
*
* Update: Downloading RSS data in an async task
*/
public class NewsApp2Activity extends Activity {
// A reference to the local object
private NewsApp2Activity local;
/**
* This method creates main application view
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.main);
// Set reference to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
// Start download RSS task
task.execute("http://rss.nytimes.com/services/xml/rss/nyt/Arts.xml");
// Debug the thread name
Log.d("ITCRssReader", Thread.currentThread().getName());
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
@Override
protected List<RssItem> doInBackground(String... urls) {
// Debug the task thread name
Log.d("ITCRssReader", Thread.currentThread().getName());
try {
// Create RSS reader
RssReader rssReader = new RssReader(urls[0]);
// Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("ITCRssReader", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<RssItem> result) {
// Get a ListView from main view
ListView itcItems = (ListView) findViewById(R.id.listMainView);
// Create a list adapter
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem> (local,android.R.layout.simple_list_item_1, result);
// Set list adapter for the ListView
itcItems.setAdapter(adapter);
// Set list view item click listener
itcItems.setOnItemClickListener(new ListListener(result, local));
}
}
}
而這裏是清單
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.testproject.newsapp2.MainScreen"
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=".NewsApp2Activity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
這裏的logcat的
02-18 16:37:09.638: E/Trace(8930): error opening trace file: No such file or directory (2)
02-18 16:37:09.638: D/ActivityThread(8930): setTargetHeapUtilization:0.25
02-18 16:37:09.638: D/ActivityThread(8930): setTargetHeapIdealFree:8388608
02-18 16:37:09.638: D/ActivityThread(8930): setTargetHeapConcurrentStart:2097152
02-18 16:37:09.778: D/libEGL(8930): loaded /system/lib/egl/libEGL_adreno200.so
02-18 16:37:09.778: D/libEGL(8930): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
02-18 16:37:09.788: D/libEGL(8930): loaded /system/lib/egl/libGLESv2_adreno200.so
02-18 16:37:09.818: I/Adreno200-EGLSUB(8930): <ConfigWindowMatch:2087>: Format RGBA_8888.
02-18 16:37:09.828: E/(8930): <s3dReadConfigFile:75>: Can't open file for reading
02-18 16:37:09.838: E/(8930): <s3dReadConfigFile:75>: Can't open file for reading
02-18 16:37:09.838: D/OpenGLRenderer(8930): Enabling debug mode 0
02-18 16:37:16.916: D/AndroidRuntime(8930): Shutting down VM
02-18 16:37:16.916: W/dalvikvm(8930): threadid=1: thread exiting with uncaught exception (group=0x41c2a438)
02-18 16:37:16.926: E/AndroidRuntime(8930): FATAL EXCEPTION: main
02-18 16:37:16.926: E/AndroidRuntime(8930): java.lang.IllegalStateException: Could not find a method gotorssfunction(View) in the activity class com.testproject.newsapp2.MainScreen for onClick handler on view class android.widget.Button with id 'button1'
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.view.View$1.onClick(View.java:3661)
2-18 16:37:16.926: E/AndroidRuntime(8930): at android.view.View.performClick(View.java:4198)
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.view.View$PerformClick.run(View.java:17158)
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.os.Handler.handleCallback(Handler.java:615)
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.os.Looper.loop(Looper.java:137)
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.app.ActivityThread.main(ActivityThread.java:4918)
02-18 16:37:16.926: E/AndroidRuntime(8930): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:37:16.926: E/AndroidRuntime(8930): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 16:37:16.926: E/AndroidRuntime(8930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
02-18 16:37:16.926: E/AndroidRuntime(8930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
02-18 16:37:16.926: E/AndroidRuntime(8930): at dalvik.system.NativeStart.main(Native Method)
2-18 16:37:16.926: E/AndroidRuntime(8930): Caused by: java.lang.NoSuchMethodException: gotorssfunction [class android.view.View]
02-18 16:37:16.926: E/AndroidRuntime(8930): at java.lang.Class.getConstructorOrMethod(Class.java:460)
02-18 16:37:16.926: E/AndroidRuntime(8930): at java.lang.Class.getMethod(Class.java:915)
02-18 16:37:16.926: E/AndroidRuntime(8930): at android.view.View$1.onClick(View.java:3654)
2-18 16:37:16.926: E/AndroidRuntime(8930): ... 11 more
02-18 16:37:25.517: I/Process(8930): Sending signal. PID: 8930 SIG: 9
對於崩潰,請從logcat發佈完整崩潰日誌 – 2013-02-19 00:33:39
感謝Gabe這裏是來自logcat – user2085187 2013-02-19 00:42:46