0

我正在尝试学习 android/java 编程的基础知识。我根据教程创建了一个基本的 RSS android 应用程序。我添加了另一个活动来创建一个新的开始屏幕,我试图链接到教程中的主要 RSS 活动,但是当我运行应用程序时,当我点击新开始屏幕上的按钮时它总是崩溃。我在这里做明显错误的事情吗?

这是我创建的新开始屏幕的 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>

这是日志猫

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
4

1 回答 1

0

错字。方法签名应该public void gotorssfunction(View v)代替public void gotorssfuction(View v).

于 2013-02-19T00:45:57.060 回答