为什么我的视图对象引用仍然为null?

时间:2016-04-25 01:59:44

标签: java android

如果我正确解释下面的错误消息,那么"视图"由于某种原因,对象似乎是null。在线查看,大多数有类似问题的人通常会在设置内容视图之前创建引用。但是,正如您在我的代码中看到的那样,这不是我的情况。我不确定问题是什么。

以下是我在尝试运行应用时遇到的错误消息(使用我自己的手机作为调试媒体,如果重要的话):

04-24 20:54:03.848: E/AndroidRuntime(13773): FATAL EXCEPTION: main
04-24 20:54:03.848: E/AndroidRuntime(13773): Process: com.example.fiddle, PID: 13773
04-24 20:54:03.848: E/AndroidRuntime(13773): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fiddle/com.example.fiddle.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.ActivityThread.access$1100(ActivityThread.java:222)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.os.Looper.loop(Looper.java:158)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.ActivityThread.main(ActivityThread.java:7229)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at java.lang.reflect.Method.invoke(Native Method)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-24 20:54:03.848: E/AndroidRuntime(13773): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
04-24 20:54:03.848: E/AndroidRuntime(13773):    at com.example.fiddle.MainActivity.onCreate(MainActivity.java:25)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.Activity.performCreate(Activity.java:6876)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
04-24 20:54:03.848: E/AndroidRuntime(13773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
04-24 20:54:03.848: E/AndroidRuntime(13773):    ... 9 more

这是我的MainActivity.java代码:

package com.example.fiddle;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout view = (RelativeLayout) findViewById(R.layout.activity_main);

        view.setOnTouchListener (new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(getBaseContext(), String.valueOf(event.getX()) + ", " + String.valueOf(event.getY()), Toast.LENGTH_LONG);
                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

以下是该视图的XML文档:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="com.example.fiddle.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您应为RelativeLayout

添加ID
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        ... 
        android:id="@+id/root_relativelayout"
        ...
       >
       ...
   </RelativeLayout>

然后在onCreate方法中,找到RelativeLayout

  protected void onCreate(Bundle savedInstanceState) {        
        ...
         //RelativeLayout view = (RelativeLayout) findViewById(R.layout.activity_main);
        RelativeLayout view = (RelativeLayout) findViewById(R.id.root_relativelayout);
        ...
  } 

答案 1 :(得分:0)

您需要在xml文件中将android:id="@+id/relative_layout"添加到RelativeLayour 并替换此行
RelativeLayout view = (RelativeLayout) findViewById(R.layout.activity_main);
RelativeLayout view = (RelativeLayout) findViewById(R.id.relative_layout);
顺便说一下,你需要仔细阅读并做更多的例子。