首页 文章

尝试在空对象引用上调用虚方法' '

提问于
浏览
1

我正在学习应用程序开发,所以我是一个初学者 . 我正在关注TheNewBoston的教程,没有语法错误但是当我运行应用程序时,我得到以下内容:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: bakaev.myapplication, PID: 3888
              java.lang.RuntimeException: Unable to start activity ComponentInfo{bakaev.myapplication/bakaev.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
                  at bakaev.myapplication.MainActivity.onCreate(MainActivity.java:24)
                  at android.app.Activity.performCreate(Activity.java:6237)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的MainActivity如下:

public class MainActivity extends AppCompatActivity {


private ViewGroup testapp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    testapp = (ViewGroup) findViewById(R.id.activity_main);

    testapp.setOnTouchListener(
            new RelativeLayout.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event){
                  moveButton();
                  return true;
                }
            }
    );
    setContentView(R.layout.activity_main);

}


public void moveButton(){

    View Button = findViewById(R.id.ChangeButton);

    TransitionManager.beginDelayedTransition(testapp);

    RelativeLayout.LayoutParams positionrules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    positionrules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    positionrules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    Button.setLayoutParams(positionrules);

    ViewGroup.LayoutParams size = Button.getLayoutParams();
    size.width = 450 ;
    size.height = 300;
    Button.setLayoutParams(size);

}

}

成绩构建是:

minSdkVersion 19
    targetSdkVersion 24

Android Manifest有:

android:theme="@style/Theme.AppCompat.Light">

添加到它 .

Activity_main.xml是:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="bakaev.myapplication.MainActivity">

<Button
        android:text="Change Me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/ChangeButton" />
</RelativeLayout>

4 回答

  • 0

    在对膨胀的xml布局进行任何引用之前,需要调用setContentView . 因此,您的OnCreate应如下所示:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    testapp = (ViewGroup) findViewById(R.id.activity_main);
    
    testapp.setOnTouchListener(
            new RelativeLayout.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event){
                  moveButton();
                  return true;
                }
            }
    );
    

    }

  • 0
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set view content before doing findViewById.
    setContentView(R.layout.activity_main)
    testapp = (ViewGroup) findViewById(R.id.activity_main);
    

    删除执行findViewById后使用的setContentview .

  • 1

    你应该在调用 findViewById 之前调用 setContentView .

    根据JavaDoc for Activity#findViewById

    查看findViewById(int id)查找由onCreate(Bundle)中处理的XML中的id属性标识的视图 .

  • 0

    使用这一行

    setContentView(R.layout.activity_main)

    之前

    testapp = (ViewGroup) findViewById(R.id.activity_main)

    在onCreate()方法中 .

相关问题