摆弄Android开发。我在Java方面有经验,但对于Android开发人员来说我很新,所以请和我一起裸照。为什么我的视图对象引用仍为空(Android开发)?
如果我正确解释下面的错误消息,“视图”对象由于某种原因似乎为空。在线观看时,大多数遇到类似问题的人通常会在设置内容视图之前创建参考。然而,正如你可以看到我的代码,这不是我的情况。我不确定问题是什么。
以下是错误消息我在尝试运行应用程序时(用我自己的手机作为媒介的调试,如果它的问题):
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>
谢谢你,让我觉得很愚蠢。 –