我尝试使用从activity1到activity2的方法。但是,当我尝试打开此活动应用程序崩溃时。
我正在学习机器人,这是一项练习。我应该在Java代码上部署onClickListener,而不是使用onClick on XML。但是因为相同的按钮在不同的活动上相互存在3倍,我尝试从另一个活动中调用一个方法,而不是再次写入3x。
我想更好地了解如何解决此问题。谢谢你的帮助
我也在这里搜索过,但一无所获。也许我无法完全理解其他代码的逻辑。
活动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);
}
});
}
}
活动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的
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)
答案 0 :(得分:5)
MainActivity mainAct = new MainActivity();
永远不要自己创建活动的实例。
如果res/layout/activity_list.xml
的{{1}}名为<Button>
,那么最简单的事情(至少在短期内)是将shopButton
复制或移动到{{} 1}},所以你可以直接在那里使用它。
如果buttonIntent()
没有名为ListActivity
的{{1}},则res/layout/activity_list.xml
中的代码与您的<Button>
无关。
shopButton
的类,这可能会让您感到困惑。