Android:如何在其他文件中使用类

Android:如何在其他文件中使用类,第1张

概述我试图为我的许多可重用方法制作一个处理程序类.我在连接单独的类文件时遇到问题.有人可以告诉我这个例子失败了吗,或者必须做些什么才能使其起作用?ClassExampleActivity.java:    包com.apollo.classexample;importandroid.app.Activity;importandroid.os.Bundle;impo

我试图为我的许多可重用方法制作一个处理程序类.我在连接单独的类文件时遇到问题.

有人可以告诉我这个例子失败了吗,或者必须做些什么才能使其起作用?

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 innerclass03-20 17:03:42.783: D/AndroIDRuntime(654): Shutting down VM03-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: main03-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.NullPointerException03-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.access00(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.NullPointerException03-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中扩展Activity类.
>您正在两次设置文本值,请从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);    }}

这应该工作!!

总结

以上是内存溢出为你收集整理的Android:如何在其他文件中使用类全部内容,希望文章能够帮你解决Android:如何在其他文件中使用类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1084677.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存