Android:尝试实例化一个类

时间:2014-02-28 21:46:43

标签: java android instantiation

我仍然是Java编程的新手,我现在正试图解决实例化问题,并遇到一些问题。我试图尽可能简单地测试这个,但是我的程序会强行关闭。

*我的班级

public class InterfaceScreen extends Activity implements testInterface1 {
TextView tv1, tv2;
String String1 = "I ate a pizza!";
String String2 = "sleeping......";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_interface_screen);
    tv1 = (TextView)findViewById(R.id.textView1);
    tv2 = (TextView)findViewById(R.id.textView3);   

    //Attempint to create a new InterfaceScreen (which is an animal by the way....)
    InterfaceScreen myAnimal = new InterfaceScreen();

    //now telling our animal to eat and sleep
    myAnimal.eat(String1);
    myAnimal.sleep(String2);

}
public void eat(String text1) {
    tv1.setText(text1);
}

public void sleep(String text2) {
    tv2.setText(text2);
}

}

*我的界面屏幕,再次让它变得尽可能简单。

public interface testInterface1 {

    void eat(String text1);
    void sleep(String text2);
}

我的活动的XML

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".InterfaceScreen" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="21dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="30dp"
    android:text="TextView" />

 </RelativeLayout>

Log Cat输出

02-28 16:03:36.621: E/AndroidRuntime(2557): FATAL EXCEPTION: main
02-28 16:03:36.621: E/AndroidRuntime(2557): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.generaljavalearning/com.example.generaljavalearning.InterfaceScreen}: java.lang.NullPointerException
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.os.Looper.loop(Looper.java:137)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at java.lang.reflect.Method.invoke(Method.java:525)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at dalvik.system.NativeStart.main(Native Method)
02-28 16:03:36.621: E/AndroidRuntime(2557): Caused by: java.lang.NullPointerException
02-28 16:03:36.621: E/AndroidRuntime(2557):     at com.example.generaljavalearning.InterfaceScreen.eat(InterfaceScreen.java:33)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at com.example.generaljavalearning.InterfaceScreen.onCreate(InterfaceScreen.java:28)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.Activity.performCreate(Activity.java:5133)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-28 16:03:36.621: E/AndroidRuntime(2557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-28 16:03:36.621: E/AndroidRuntime(2557):     ... 11 more

2 个答案:

答案 0 :(得分:2)

onCreate中,您已在InterfaceScreen的实例内操作。它被称为this。您无需创建新实例。

而不是

myAnimal.eat(String1);
myAnimal.sleep(String2);

只是说

this.eat(String1);
this.sleep(String2);

甚至更好

eat(String1);
sleep(String2);

此外,变量通常以小写字母开头:string1string2。类以大写字母开头。

NullPointerException正在发生,因为您的新实例未获得正确的tv1tv2引用。在Android中,我们不会创建Activity的实例。

答案 1 :(得分:0)

不完全确定你在这里尝试做什么,但你没有实例化Activity,就像你做普通的Java类一样。所以这是错误的

InterfaceScreen myAnimal = new InterfaceScreen();

你只需要打电话给

eat(String1);

看起来你可能想要一个名为Animal之类的普通Java类。然后你可以像尝试一样实例化它并设置/获取属性。但只是为了你正在做的事情,你可以通过调用方法来做。