简体   繁体   English

Android-尝试从onCreate()中的另一个类调用方法

[英]Android - Trying to call a method from another class inside onCreate()

I'm trying to use a method from activity1 to activity2. 我正在尝试使用从activity1到activity2的方法。 But, when I try to open this activity app crashes. 但是,当我尝试打开此活动时,应用程序崩溃。

I'm learning android, it's a exercise. 我正在学习android,这是一种练习。 Instead of use onClick on XML I should deploy onClickListener on Java code. 我应该在Java代码上部署onClickListener,而不是在XML上使用onClick。 But as the same button will exist 3x on diferents activity doing Intents each other, I tryied just to call a method from another activity instead of write the same again 3x. 但是由于同一个按钮在不同活动之间互为Intent时将存在3x,因此我尝试仅从另一个活动中调用方法,而不是再将其写为3x。

I would like to understand better how could I fix this issue. 我想更好地了解如何解决此问题。 Thanks for any help 谢谢你的帮助

I also have searched here, but have found nothing. 我也在这里搜索,但什么也没找到。 Maybe I can't understand the logic from anothers codes completely. 也许我无法完全理解他人代码的逻辑。

Activity 1 (snippet) 活动1(摘要)

//Do intent on Button shopButton
public void buttonIntent(){
    Button buttonShop = findViewById(R.id.shopButton);
    if (buttonShop != null){
        buttonShop.setOnClickListener(new OnClickListener( ) {
            @Override
            public void onClick( View v ) {
                Intent i = new Intent(MainActivity.this,ShopActivity.class);
                startActivity(i);
            }
        });
    }
}

Activity 2 活动2

package com.example.android.musique;

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

public class ListActivity extends AppCompatActivity {

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

        //Create an MainActivity Object
        MainActivity mainAct = new MainActivity();

        //Call methods what catches the button and do intent from MainActivity
        mainAct.buttonIntent();
    }

}

LOGCAT LOGCAT

01-10 22:24:16.514 8887-8956/com.example.android.musique D/OpenGLRenderer: ProgramCache.generateProgram: 34359738371
01-10 22:24:16.516 8887-8956/com.example.android.musique D/OpenGLRenderer: ProgramCache.generateProgram: 240518168576
01-10 22:24:16.518 8887-8956/com.example.android.musique D/OpenGLRenderer: ProgramCache.generateProgram: 68724719680
01-10 22:24:16.529 8887-8956/com.example.android.musique D/GraphicBuffer: register, handle(0x7a2b9b5780) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-10 22:24:16.539 8887-8887/com.example.android.musique V/InputMethodManager: onWindowFocus: null softInputMode=272 first=true flags=#81810100
01-10 22:24:16.564 8887-8956/com.example.android.musique D/GraphicBuffer: register, handle(0x7a2b9b6200) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-10 22:24:17.401 8887-8894/? I/art: Ignoring second debugger -- accepting and dropping
01-10 22:24:17.829 8887-8956/? D/OpenGLRenderer: ProgramCache.generateProgram: 34359738369
01-10 22:24:18.383 8887-8956/? D/OpenGLRenderer: CacheTexture 7 upload: x, y, width height = 72, 18, 28, 103
01-10 22:24:18.392 8887-8956/? D/OpenGLRenderer: CacheTexture 7 upload: x, y, width height = 84, 120, 17, 90
01-10 22:24:18.408 8887-8956/? D/OpenGLRenderer: CacheTexture 7 upload: x, y, width height = 84, 209, 15, 24
01-10 22:24:18.507 8887-8956/? D/OpenGLRenderer: ProgramCache.generateProgram: 103079215104
01-10 22:24:19.352 8887-8956/? D/OpenGLRenderer: ProgramCache.generateProgram: 103084458052
01-10 22:24:24.085 8887-8887/? D/AndroidRuntime: Shutting down VM
01-10 22:24:24.095 8887-8887/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.android.musique, PID: 8887
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.musique/com.example.android.musique.ListActivity}: 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:2745)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529)
                                                     at android.os.Handler.dispatchMessage(Handler.java:110)
                                                     at android.os.Looper.loop(Looper.java:203)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6275)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
                                                  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.<init>(AppCompatDelegateImplBase.java:117)
                                                     at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
                                                     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
                                                     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
                                                     at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
                                                     at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
                                                     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                     at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                     at com.example.android.musique.MainActivity.buttonIntent(MainActivity.java:45)
                                                     at com.example.android.musique.ListActivity.onCreate(ListActivity.java:19)
                                                     at android.app.Activity.performCreate(Activity.java:6717)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2698)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                     at android.os.Looper.loop(Looper.java:203) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6275) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
MainActivity mainAct = new MainActivity();

Never create an instance of an activity yourself. 切勿自己创建活动的实例。

If res/layout/activity_list.xml has a <Button> named shopButton , then the simplest thing to do (at least in the short term) is to copy or move buttonIntent() into ListActivity , so you can use it directly there. 如果res/layout/activity_list.xml具有名为shopButton<Button> ,那么最简单的做法(至少在短期内)是将buttonIntent()复制或移动到ListActivity ,因此您可以直接在其中使用它。

If res/layout/activity_list.xml does not have a <Button> named shopButton , then the code in buttonIntent() is not relevant to your ListActivity . 如果res/layout/activity_list.xml没有名为shopButton<Button> ,则buttonIntent()的代码与ListActivity不相关。

BTW, the Android SDK already has a class named ListActivity , which may cause you some confusion. 顺便说一句,Android SDK已经有一个名为ListActivity的类,这可能会引起您一些困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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