擺弄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>
謝謝你,讓我覺得很愚蠢。 –