简体   繁体   English

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

[英]Why is my view object reference still null?

If I'm interpreting the error message below correctly, the "view" object seems to be null for some reason.如果我正确解释下面的错误消息,“视图”对象由于某种原因似乎为空。 Looking online, most people who had a similar problem usually created the reference before setting the content view.在网上查看,大多数遇到类似问题的人通常在设置内容视图之前创建参考。 However, as you can see by my code, this is not my case.但是,正如您从我的代码中看到的,这不是我的情况。 I'm not sure what the problem is.我不确定问题是什么。

Here is the error message I get when trying to run the app (using my own phone as a debugging medium, if it matters):这是我在尝试运行应用程序时收到的错误消息(使用我自己的手机作为调试媒介,如果重要的话):

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

Here is my MainActivity.java code:这是我的 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);
    }
}

And here is the XML document for the view:这是视图的 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>

You should add an id for your RelativeLayout您应该为您的RelativeLayout添加一个 id

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

then in onCreate method, find the RelativeLayout by然后在onCreate方法,找到RelativeLayout通过

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

You need to add android:id="@+id/relative_layout" to RelativeLayour in xml file.您需要在 xml 文件中将android:id="@+id/relative_layout"到 RelativeLayour 。
And replace this line并替换这一行
RelativeLayout view = (RelativeLayout) findViewById(R.layout.activity_main);
with RelativeLayout view = (RelativeLayout) findViewById(R.id.relative_layout);使用RelativeLayout view = (RelativeLayout) findViewById(R.id.relative_layout);
Btw, you need to read and do more example carefully.顺便说一句,你需要仔细阅读并做更多的例子。

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

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