【发布时间】:2012-03-19 09:25:29
【问题描述】:
我正在尝试为我的许多可重用方法创建一个处理程序类。我在连接单独的类文件时遇到问题。
谁能告诉我这个例子失败的地方或者需要做什么才能让它工作?
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 mh = new myhandler();活动。
-
在 OnCreate() 方法中初始化布局之前您不能使活动的实例您可能必须启动 Activity 覆盖 oncreate 方法通过 SetContentView() 设置其内容然后设置其视图的属性.
-
Nitin 我正在努力实施您的建议。