2013-07-31 71 views
-1

我是一名Android初學者我正在開發一個應用程序,它有兩個屏幕,當我嘗試從一個切換到另一個應用程序崩潰時,有人可以告訴我爲什麼發生,是那是因爲第二課的聲明?我已經在清單文件中添加了第二個類,這似乎並不是問題。 主類是這樣的: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 
+0

格式化你的問題。 – RobinHood

+0

理解日誌非常重要,在com.example.treedb.dbManager.onCreate(dbManager.java:30)處查看這一行',因爲您看到所有內容都在日誌中,所以您只需要閱讀它仔細。 – EvZ

+0

它顯然是一個空指針的問題,所以檢查第30行。 –

回答

3

list1.addAll(dh1.getAllItems());

此代碼來自dbManager的活動課給你NullPointerException

因爲您正在使用dh1對象沒有初始化它。所以它會是NULL

+0

yup !!! thanx兄弟現在的作品! – insomniac

0

您忘記了在dbManager活動內部初始化您的數據庫幫助器類,它會拋出空指針。

初始化它。

dh1 = new DatabaseH(this);

1

在線list1.addAll(dh1.getAllItems());您嘗試tu使用dh1,但您沒有初始化它。 試試這個:

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