Android无法添加窗口错误

时间:2013-08-23 00:27:37

标签: android

我已经写了一些代码来为我的应用程序使用我的自定义对话框,我认为它一直顺利,直到我强行崩溃。我看过我的logcat,它说它无法添加窗口所以我只是希望有人可以帮助我解决这个问题。

logcat的:

08-23 02:05:22.836: E/AndroidRuntime(898): FATAL EXCEPTION: main
08-23 02:05:22.836: E/AndroidRuntime(898): java.lang.NullPointerException
08-23 02:05:22.836: E/AndroidRuntime(898):  at com.theproblemsolver.MainActivity$1.run(MainActivity.java:42)
08-23 02:05:22.836: E/AndroidRuntime(898):  at android.os.Handler.handleCallback(Handler.java:725)
08-23 02:05:22.836: E/AndroidRuntime(898):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 02:05:22.836: E/AndroidRuntime(898):  at android.os.Looper.loop(Looper.java:137)
08-23 02:05:22.836: E/AndroidRuntime(898):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-23 02:05:22.836: E/AndroidRuntime(898):  at java.lang.reflect.Method.invokeNative(Native Method)
08-23 02:05:22.836: E/AndroidRuntime(898):  at java.lang.reflect.Method.invoke(Method.java:511)
08-23 02:05:22.836: E/AndroidRuntime(898):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-23 02:05:22.836: E/AndroidRuntime(898):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-23 02:05:22.836: E/AndroidRuntime(898):  at dalvik.system.NativeStart.main(Native Method)

MainActivity:

public class MainActivity extends Activity {

    TextView TPS;
    TextView TPS1;
    TextView TPS2;
    TextView TPS3;
    Button TPSbutton1;
    Button getAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View mainView = getLayoutInflater().inflate(R.layout.activity_main, null);

        setContentView(mainView);

        final Dialog dialog = new Dialog(this); 
        dialog.setContentView(R.layout.customdialog);
        dialog.setTitle("TheProblemSolver");
        mainView.post(new Runnable() {
            public void run() {
                dialog.show();
                 TPS = (TextView) findViewById(R.id.TPS);
                 TPS1 = (TextView) findViewById(R.id.TPS1);
                 TPS2 = (TextView) findViewById(R.id.TPS2);
                 TPS3 = (TextView) findViewById(R.id.TPS3);
                 TPSbutton1 = (Button) findViewById(R.id.TPSbutton1);

                 TPSbutton1.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                 }               
                         );         

            }
        });

         final EditText et = (EditText) findViewById(R.id.editText1);

        Button getAnswer = (Button) findViewById(R.id.button1);
        getAnswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {       
            if (et.getText().toString().length()==0) {
                Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

            }else{

            EditText et = (EditText) findViewById(R.id.editText1);
            String searchTerm = et.getText().toString().trim();         
            Intent in = new Intent(MainActivity.this, ListView.class);
            in.putExtra("TAG_SEARCH", searchTerm);
            startActivity(in);
        }

        }});
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

}

customdialog.xml:

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

    <TextView
        android:id="@+id/TPS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        android:text="example"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/TPS1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.02"
        android:text="example"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TPS2"
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:text="example"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/TPS3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.02"
        android:text="example"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/TPSbutton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OK" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

findViewById()中的小错误。

TPS = (TextView) findViewById(R.id.TPS);
                 TPS1 = (TextView) dialog.findViewById(R.id.TPS1);
                 TPS2 = (TextView) dialog.findViewById(R.id.TPS2);
                 TPS3 = (TextView) dialog.findViewById(R.id.TPS3);
                 TPSbutton1 = (Button) dialog.findViewById(R.id.TPSbutton1);