gpt4 book ai didi

Android:如何在不同的文件中使用类

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:53 24 4
gpt4 key购买 nike

我正在尝试为我的许多可重用方法创建一个处理程序类。我在连接单独的类文件时遇到问题。

谁能告诉我这个例子失败的地方或者必须做些什么才能让它工作?

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);
}
}

我的处理程序.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。

日志:

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

最佳答案

  1. 将名称 myhandler 更改为“MyHandler”,这听起来不错..
  2. 在目前的情况下,您不需要扩展 MyHandler 中的 Activity 类。
  3. 您正在两次设置文本值,从测试方法中删除以下不需要的行..

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);


}
}

这应该行得通!!

关于Android:如何在不同的文件中使用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9767657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com