简体   繁体   中英

How do you call a class (that's not an activity) in an activity class in Android Studio?

I'm learning Android Studio and I decided to create a Java class and then call it in MainActivity. However, the app crashes on startup - see below. I just don't understand what the error means. Any thoughts?

MainActivity.java

   package com.example.daniel.hamblaster;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        generateText obj = new generateText();
        obj.generate();

    }
}

Java class:

package com.example.daniel.hamblaster;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class generateText extends AppCompatActivity {

    Button myButton = (Button) findViewById(R.id.myButton);

    public void generate() {
        myButton.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    TextView myText = (TextView) findViewById(R.id.myText);
                    myText.setText("blablaba");
                }
            }
        );
    }
}

Error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.daniel.hamblaster, PID: 5560 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.daniel.hamblaster/com.example.daniel.hamblaster.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.Zygot eInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:120) at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:151) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:31) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:55) at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:33) at android.support.v7.app.AppCompatDelegateImplN.(AppCompatDelegateImplN.java:33) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525) at android.support.v7.app.AppCompatActivity. findViewById(AppCompatActivity.java:193) at com.example.daniel.hamblaster.generateText.(generateText.java:9) at com.example.daniel.hamblaster.MainActivity.onCreate(MainActivity.java:14) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Application terminated.

You are trying to make impossible stuff.

Activities are not to be created as an ordinary class. I can see that you are starting to get a grip of what Java is. Take your time and learn Java basics before running into Android.

For short: Activities are not to be instantiated with new Activity(); If you are trying to, please, use Intents.

Intent a = new Intent(this, ActivityB.class);
this.startActivity(a);

This is the way to open an activity from another.

And if you REALLY want to just instantiate a class, remove that extension from generateText class and just handle it just like a normal and ordinary class.

You should, as well, check some Java code standards :) Do never create a Class with lowercase first letter.

Best of luck.

1) If you are working with UI, do it in the activity you are currently in.

2) If you want to start another activity, use:

Intent intent = new Intent(ActivityA.this, ActivityB.class); startActivity(intent);

3) If you want to execute a method of another class, let it be

public static <return-type> method() {...} in that class. This way you even do not need to initialize you class (make it static too, btw).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM