android layoutinflater Null Exception

时间:2012-02-01 07:03:33

标签: android android-layout

我正在尝试创建布局inflater,但应用程序崩溃并在log cat中出现NullException错误...

这是我的代码......

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="100dp"
android:background="#DAAA">        
      <TextView
    android:id="@+id/customt2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"/>
</LinearLayout> 

和程序代码......

      final ImageView text2 = (ImageView) findViewById(R.id.text2);
    text2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LayoutInflater inflater2 = getLayoutInflater();
            View layout2 = inflater2.inflate(R.layout.custom_layout, 
                    (ViewGroup)findViewById(R.id.toast_layout));
            TextView textd2 = (TextView)findViewById(R.id.customt2);
                textd2.setText(R.string.slide2);

            Toast toast2 = new Toast(getApplicationContext());
            toast2.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
            toast2.setDuration(Toast.LENGTH_LONG);
            toast2.setView(layout2);
            toast2.show();

        }
    }); 

错误......

02-01 10:55:10.936: W/dalvikvm(17939): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
02-01 10:55:10.956: E/AndroidRuntime(17939): FATAL EXCEPTION: main
02-01 10:55:10.956: E/AndroidRuntime(17939): java.lang.NullPointerException
02-01 10:55:10.956: E/AndroidRuntime(17939):    at com.home.Main$15.onClick(Main.java:450)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at android.view.View.performClick(View.java:2408)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at android.view.View$PerformClick.run(View.java:8818)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at android.os.Handler.handleCallback(Handler.java:587)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at android.os.Looper.loop(Looper.java:123)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at java.lang.reflect.Method.invokeNative(Native Method)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at java.lang.reflect.Method.invoke(Method.java:521)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-01 10:55:10.956: E/AndroidRuntime(17939):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

复制代码时是错误的,还是在添加textview之前结束了线性布局?

键入<LinearLayout />

与输入

相同
<LinearLayout ></LinearLayout>

删除斜杠并尝试您的程序。

正确的代码应该是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="100dp"
android:background="#DAAA">        
      <TextView
    android:id="@+id/customt2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"/>
</LinearLayout> 

答案 1 :(得分:1)

替换

TextView textd2 = (TextView)findViewById(R.id.customt2);

使用

TextView textd2 = (TextView)layout2.findViewById(R.id.customt2);