问题描述
我试图做一个处理类对于很多可重复使用的方法。我有接口的独立的类文件的问题。
有人能告诉我这个例子中出现故障或有什么会做使它工作?
ClassExampleActivity.java:
包com.apollo.classexample;
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;
公共类ClassExampleActivity延伸活动{
/ **当第一次创建活动调用。 * / TextView的信息文本; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
字符串infostring =主要活动类\\ N的; 将myHandler MH =新将myHandler();
infostring + = mh.testmethod(); //尝试使用TextView的部件从这个类MyHandler的
//这个工程由主类,但在类MyHandler的失败
TextView的信息文本=(的TextView)findViewById(R.id.infotext);
infotext.setText(infostring);
}
}
Myhandler.java:
包com.apollo.classexample;
进口android.widget.TextView;
进口android.app.Activity;公共类将myHandler延伸活动{ 公共字符串TestMethod的(){
字符串innermessage =这是从内部类\\ n的消息;
的System.out.println(innermessage); / *这是我想在这个类中使用的重要组成部分
在谈到这些行并应用不会崩溃。
我想学习如何使用接下来的两行MyHandler的这
其中我试图让便携和可重复使用的其他类
projectes。
* /
TextView的信息文本=(的TextView)findViewById(R.id.infotext);
infotext.setText(innermessage); //放置到表明MyHandler的类被实际访问
返回innermessage;
}}
AndroidManifest.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.apollo.classexample
安卓版code =1
机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =7/> <应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名字=。ClassExampleActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=MyHandler的/>
<活动机器人:名字=MyCRUDSQL/> < /用途>< /清单>
在调用TestMethod的()的Android应用程序会崩溃。如果我注释掉它会运行和打印System.out.printlin()消息LogCat中。
LogCat中:
03-20 17:03:42.783:我/的System.out(654):这是从内部类的消息
03-20 17:03:42.783:D / AndroidRuntime(654):关闭VM
03-20 17:03:42.803:W / dalvikvm(654):主题ID = 1:螺纹未捕获的异常(组= 0x40014760)退出
03-20 17:03:42.853:E / AndroidRuntime(654):致命异常:主要
03-20 17:03:42.853:E / AndroidRuntime(654):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.apollo.classexample / com.apollo.classexample.ClassExampleActivity}:显示java.lang.NullPointerException
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.ActivityThread.access $ 1500(ActivityThread.java:122)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1002)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.os.Handler.dispatchMessage(Handler.java:99)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.os.Looper.loop(Looper.java:132)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.ActivityThread.main(ActivityThread.java:4025)
03-20 17:03:42.853:E / AndroidRuntime(654):在java.lang.reflect.Method.invokeNative(本机方法)
03-20 17:03:42.853:E / AndroidRuntime(654):在java.lang.reflect.Method.invoke(Method.java:491)
03-20 17:03:42.853:E / AndroidRuntime(654):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
03-20 17:03:42.853:E / AndroidRuntime(654):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-20 17:03:42.853:E / AndroidRuntime(654):在dalvik.system.NativeStart.main(本机方法)
03-20 17:03:42.853:E / AndroidRuntime(654):致:显示java.lang.NullPointerException
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.Activity.findViewById(Activity.java:1744)
03-20 17:03:42.853:E / AndroidRuntime(654):在com.apollo.classexample.MyHandler.testmethod(MyHandler.java:16)
03-20 17:03:42.853:E / AndroidRuntime(654):在com.apollo.classexample.ClassExampleActivity.onCreate(ClassExampleActivity.java:23)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-20 17:03:42.853:E / AndroidRuntime(654):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
03-20 17:03:42.853:E / AndroidRuntime(654):11 ...更多
- 更改名称将myHandler为 MyHandler的,这听起来不错..
- 在present情况下,你不需要在扩展Activity类的 MyHandler的
- 您要设置文本值的两倍删除以下从TestMethod的线,他们并不需要他们。
TextView的信息文本=(的TextView)findViewById(R.id.infotext);
infotext.setText(innermessage);
尝试更新你的问题,如果你得到任何错误....
修改
//MyHandler.java
公共类将myHandler { 公共无效TestMethod的(TextView的txtView){
串innermessage =这是从内部类的消息。;
txtView.setText(innerMessage); } }
// ClassExampleActivity.java
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;公共类ClassExampleActivity延伸活动{
TextView的信息文本; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
字符串infostring =主要活动类\\ N的;
TextView的信息文本=(的TextView)findViewById(R.id.infotext);
infotext.setText(infostring); 将myHandler MH =新将myHandler();
mh.testMethod(信息文本);
}
}
这应该工作!
I'm trying to make a handler class for many of my reusable methods. I'm having problems interfacing the separate class file.
Can someone tell me what this example fails or what would have to be done to make it work?
ClassExampleActivity.java: package 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: package 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>
When calling the testmethod() the android app will crash. If I comment it out it'll run and print the System.out.printlin() message to the 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
- Change the name myhandler to 'MyHandler', that sounds nice..
- In present situation you dont need to extend Activity class in MyHandler.
- You are setting text value twice delete following lines from testmethod they are not needed their..
TextView infotext = (TextView) findViewById(R.id.infotext);
infotext.setText(innermessage);
try and update your question if you get any error....
EDIT
//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);
}
}
This should work!!
这篇关于Android的:如何使用类不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!