Eclipse Android Layout NullPointerException

时间:2014-02-21 08:08:30

标签: android eclipse layout nullpointerexception

当我尝试在Eclipse的图形布局编辑器中查看Android布局文件时,出现错误"java.lang.NullPointerException: Exception details are logged in Window > Show View > Error Log",其中错误日志只显示"main.xml: java.lang.NullPointerException."   只有这一个特定的项目 - 我已将此 main.xml 移动到另一个项目,图形编辑器工作,我已将其他(工作)布局移动到此项目中,并且它们已停止工作

我尝试过Android - >修复项目属性,但这没有帮助,我不知道哪些其他设置会影响这一点。任何关于从哪里开始寻找的建议都将非常感激!

编辑:添加了代码。此外,这用于编译和运行成功,但我在MainActivity.java文件中添加了fileLabel TextView,现在它在fileLabel.setText(timestamp)行崩溃并出现NullPointerException(下面发布了LogCat)。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Start" />

    <Button
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Stop" />

</LinearLayout>

<TextView
    android:id="@+id/fileRecordingLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="File being recorded: "
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/fileLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="11dp"
    android:text="Oct 22, 2013 12:50:30"
    android:textAppearance="?android:attr/textAppearanceSmall" />

MainActivity.java

@EActivity (R.layout.main)
public class MainActivity extends Activity {
@ViewById
TextView lblWritingTo;
@ViewById
TextView fileLabel;

@Click
public void btnStart() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String timestamp = new Timestamp(new Date().getTime()).toString().replace(' ', '_').replace(':', '-');
        String filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath()+"/"+timestamp+".wav";

        lblWritingTo.setText("Writing to "+filepath);
        fileLabel.setText(timestamp);
    } else
        Toast.makeText(this, "Can't access external storage", Toast.LENGTH_LONG).show();
}

logcat的:

02-24 02:12:44.192: E/AndroidRuntime(1595): FATAL EXCEPTION: main
02-24 02:12:44.192: E/AndroidRuntime(1595): Process: com.camdroid.test, PID: 1595
02-24 02:12:44.192: E/AndroidRuntime(1595): java.lang.NullPointerException
02-24 02:12:44.192: E/AndroidRuntime(1595):     at com.camdroid.test.MainActivity.btnStart(MainActivity.java:52)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at com.camdroid.test.MainActivity_$1.onClick(MainActivity_.java:47)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at android.view.View.performClick(View.java:4424)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at android.view.View$PerformClick.run(View.java:18383)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at android.os.Handler.handleCallback(Handler.java:733)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at android.os.Looper.loop(Looper.java:137)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-24 02:12:44.192: E/AndroidRuntime(1595):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

你清理项目吗?
项目 - &gt;清理(​​选择你的项目)

答案 1 :(得分:0)

清理项目并构建。

project --> clean

清理项目后重新启动eclipse。