3

I have wrote some code to use my custom dialog for my application and I thought it was going well until I had force crashed. I had looked at my logcat and it says that it was unable to add window so I was just hoping somebody can assist me to solve this problem.

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>
4

1 回答 1

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);
于 2013-08-23T04:23:42.840 回答