当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Attempt to invoke virtual method ' ' on a null object reference

问题描述:

I'm Currently Learning App Development so I'm quite a beginner. I'm following TheNewBoston's Tutorial, There are no syntax errors but When i run the app I get the following:

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)

My MainActivity is as follows:

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);

}

}

The Grade Build is:

 minSdkVersion 19

targetSdkVersion 24

The Android Manifest has:

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

Added To it.

Activity_main.xml is:

<?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>

网友答案:

setContentView needs to be called before making any references to the inflated xml layout. Therefore, your OnCreate should look like this:

@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;
            }
        }
);

}

网友答案:
@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);

Remove setContentview which is used after doing findViewById.

网友答案:

You should call setContentView before calling findViewById.

As per JavaDoc for Activity#findViewById

View findViewById (int id)

Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).

网友答案:

use this line

setContentView(R.layout.activity_main)

before

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

in onCreate() method.

分享给朋友:
您可能感兴趣的文章:
随机阅读: