我正在尝试为我的许多可重用方法创建一个处理程序类。我在连接单独的类文件时遇到问题。
有人能告诉我这个例子失败了什么,或者需要做些什么才能奏效吗?
classexampleactivity.java:
包com.apollo.classexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ClassExampleActivity extends Activity {
/** Called when the activity is first created. */
TextView infotext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String infostring = "Main Activity Class\n";
Myhandler mh = new Myhandler();
infostring += mh.testmethod();
// Trying to use the TextView widget from this MyHandler class
// This works from the main class but fails in the MyHandler class
TextView infotext = (TextView) findViewById(R.id.infotext);
infotext.setText(infostring);
}
}
myhandler.java文件:
包com.apollo.classexample;
import android.widget.TextView;
import android.app.Activity;
public class Myhandler extends Activity {
public String testmethod(){
String innermessage = "This is a message from the innerclass\n";
System.out.println(innermessage);
/* This is an important component that I would like to use in this class
Commenting these lines out and the application doesn't crash.
I'm trying to learn how to use the next two lines in this MyHandler
class which I'm trying to make portable and reusable for other
projectes.
*/
TextView infotext = (TextView) findViewById(R.id.infotext);
infotext.setText(innermessage);
// Placed to show that the MyHandler class is actually accessed
return innermessage;
}
}
androidmanifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apollo.classexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ClassExampleActivity"
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="MyHandler" />
<activity android:name="MyCRUDSQL" />
</application>
</manifest>
调用testMethod()时,Android应用程序将崩溃。如果我注释掉它,它将运行并将system.out.printlin()消息打印到logcat。
LogCat:
03-20 17:03:42.783: I/System.out(654): This is a message from the innerclass
03-20 17:03:42.783: D/AndroidRuntime(654): Shutting down VM
03-20 17:03:42.803: W/dalvikvm(654): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-20 17:03:42.853: E/AndroidRuntime(654): FATAL EXCEPTION: main
03-20 17:03:42.853: E/AndroidRuntime(654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apollo.classexample/com.apollo.classexample.ClassExampleActivity}: java.lang.NullPointerException
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.os.Looper.loop(Looper.java:132)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.ActivityThread.main(ActivityThread.java:4025)
03-20 17:03:42.853: E/AndroidRuntime(654): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 17:03:42.853: E/AndroidRuntime(654): at java.lang.reflect.Method.invoke(Method.java:491)
03-20 17:03:42.853: E/AndroidRuntime(654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-20 17:03:42.853: E/AndroidRuntime(654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-20 17:03:42.853: E/AndroidRuntime(654): at dalvik.system.NativeStart.main(Native Method)
03-20 17:03:42.853: E/AndroidRuntime(654): Caused by: java.lang.NullPointerException
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.Activity.findViewById(Activity.java:1744)
03-20 17:03:42.853: E/AndroidRuntime(654): at com.apollo.classexample.MyHandler.testmethod(MyHandler.java:16)
03-20 17:03:42.853: E/AndroidRuntime(654): at com.apollo.classexample.ClassExampleActivity.onCreate(ClassExampleActivity.java:23)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-20 17:03:42.853: E/AndroidRuntime(654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
03-20 17:03:42.853: E/AndroidRuntime(654): ... 11 more
最佳答案
将名称myhandler改为'myhandler',听起来不错..
在当前情况下,不需要在myhandler中扩展活动类。
您正在设置文本值两次从testmethod中删除以下行它们不需要它们的..
textView infoText=(textView)findViewByID(r.id.infoText);
infotext.setText(innermessage);
如果有任何错误,请尝试更新您的问题….
编辑
//MyHandler.java
public class Myhandler {
public void testMethod(TextView txtView){
String innermessage = "This is a message from the innerclass.";
txtView.setText(innerMessage);
}
}
//classexampleactivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ClassExampleActivity extends Activity {
TextView infotext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String infostring = "Main Activity Class\n";
TextView infotext = (TextView) findViewById(R.id.infotext);
infotext.setText(infostring);
Myhandler mh = new Myhandler();
mh.testMethod(infoText);
}
}
这应该管用!!