Android - 切换意图,应用程序意外崩溃

时间:2013-07-31 07:02:09

标签: android android-intent

我是android的初学者我正在开发一个有两个屏幕的应用程序当我尝试从一个切换到另一个应用程序崩溃任何人都可以告诉我它为什么会发生,是因为第二个声明类?我已经在清单文件中添加了第二个类,这似乎不是问题所在。 主要课程是这样的:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final DatabaseH dh =new DatabaseH(this);
        final EditText ed1=(EditText)findViewById(R.id.editText1);
        Button b1=(Button)findViewById(R.id.button1);
        Button b2=(Button)findViewById(R.id.button2);
        final String[] itemsn = new String[]{"Cat","Dog","Mouse","fish"};
       final ListView listView = (ListView) findViewById(R.id.listView1);
       final TextView tv1 = (TextView)findViewById(R.id.textView1);
       final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
       listView.setAdapter(adapter);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dh.additem(ed1.getText().toString());

            }
        });
        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //String[] values = new String[dh.getAllContacts().size()];
                //values=dh.getAllContacts().toArray(values);
                // TODO Auto-generated method stu
                adapter.clear();
        adapter.addAll(dh.getAllItems());       
        //tv1.setText(dh.getAllContacts().toString());
            }});
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                //deleting the item at clicked position
                tv1.setText(listView.getItemAtPosition(arg2).toString());
                dh.deleteContact(listView.getItemAtPosition(arg2).toString());
                adapter.clear();
                adapter.addAll(dh.getAllItems());
                // TODO Auto-generated method stub
                return false;
            }
        });
        Button b3=(Button)findViewById(R.id.admin);
        b3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                Intent myIntent = new Intent(v.getContext(), dbManager.class);
                startActivityForResult(myIntent, 0);}
                catch(Exception e){
                    tv1.setText(e.toString());
                }
            }
        });
    }



    @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;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case R.id.action_settings:

            break;
            }
        return super.onOptionsItemSelected(item);

    }

}

和第二节课:

public class dbManager extends Activity  {@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.db_manager);
    final DatabaseH dh1 = null;
    final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
    final EditText itn=(EditText)findViewById(R.id.itemname);
    final EditText cos=(EditText)findViewById(R.id.cost);
    final EditText avai=(EditText)findViewById(R.id.status);
    final RatingBar rtb1=(RatingBar)findViewById(R.id.rating);
    Button submit=(Button)findViewById(R.id.sub);
    Button add =(Button)findViewById(R.id.Addnew);
    List<String> list1 = new ArrayList<String>();
    list1.addAll(dh1.getAllItems());
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list1);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(dataAdapter);

        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            dh1.addSubitem(itn.getText().toString(),cos.getText().toString(),avai.getText().toString(),sp1.getSelectedItem().toString(),rtb1.getRating());  
            }
        });
}


}

这是我的logcat o / p:

07-31 12:29:38.227: W/dalvikvm(528): threadid=1: thread exiting with uncaught exception (group=0x40014760)
07-31 12:29:38.246: E/AndroidRuntime(528): FATAL EXCEPTION: main
07-31 12:29:38.246: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.treedb/com.example.treedb.dbManager}: java.lang.NullPointerException
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.os.Looper.loop(Looper.java:126)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.ActivityThread.main(ActivityThread.java:3997)
07-31 12:29:38.246: E/AndroidRuntime(528):  at java.lang.reflect.Method.invokeNative(Native Method)
07-31 12:29:38.246: E/AndroidRuntime(528):  at java.lang.reflect.Method.invoke(Method.java:491)
07-31 12:29:38.246: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-31 12:29:38.246: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-31 12:29:38.246: E/AndroidRuntime(528):  at dalvik.system.NativeStart.main(Native Method)
07-31 12:29:38.246: E/AndroidRuntime(528): Caused by: java.lang.NullPointerException
07-31 12:29:38.246: E/AndroidRuntime(528):  at com.example.treedb.dbManager.onCreate(dbManager.java:30)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-31 12:29:38.246: E/AndroidRuntime(528):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
07-31 12:29:38.246: E/AndroidRuntime(528):  ... 11 more

3 个答案:

答案 0 :(得分:3)

list1.addAll(dh1.getAllItems());

来自dbManager活动类的此代码行为您提供 NullPointerException

正在使用dh1对象而不进行初始化。所以它将是 NULL

答案 1 :(得分:1)

在第list1.addAll(dh1.getAllItems());行尝试使用dh1,但您没有初始化它。 试试这个:

DatabaseH dh1 =new DatabaseH(this);
list1.addAll(dh1.getAllItems());

答案 2 :(得分:0)

您忘记在dbManager活动中初始化数据库助手类,该类会抛出空指针。

初始化它。

  

dh1 = new DatabaseH(this);