-1

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

3 回答 3

3

list1.addAll(dh1.getAllItems());

dbManagerActivity 类中的此代码行为您提供NullPointerException

当您使用dh1对象而不初始化它时。所以它将是NULL

于 2013-07-31T07:05:25.723 回答
1

list1.addAll(dh1.getAllItems());您尝试使用 dh1 的行中,但您没有对其进行初始化。尝试这个:

DatabaseH dh1 =new DatabaseH(this);
list1.addAll(dh1.getAllItems());
于 2013-07-31T07:11:01.743 回答
0

您忘记在引发空指针的 dbManager 活动中初始化数据库帮助程序类。

初始化它。

dh1 = 新数据库H(this);

于 2013-07-31T07:08:22.813 回答