2014-03-28 58 views
1

我有教程視頻的代碼,運行時自動關閉應用程序。我試圖調試,並得到下面的錯誤 -Android Studio開發錯誤

03-27 23:42:14.538 1330-1330/com.testing W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3ababa8) 
03-27 23:42:14.598 1330-1330/com.testing E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.testing, PID: 1330 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing/com.testing.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
      at android.app.ActivityThread.access$800(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.testing.MainActivity.onCreate(MainActivity.java:35) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
            at android.app.ActivityThread.access$800(ActivityThread.java:135) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5017) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
            at dalvik.system.NativeStart.main(Native Method) 

package com.testing; 

    import android.support.v7.app.ActionBarActivity; 
    import android.support.v7.app.ActionBar; 
    import android.support.v4.app.Fragment; 
    import android.os.Bundle; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.os.Build; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TabHost; 

    public class MainActivity extends ActionBarActivity { 

     EditText Nome, Phone, Email; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 


      Nome = (EditText) findViewById(R.id.txtNome); 
      Phone = (EditText) findViewById(R.id.txtPhone); 
      Email = (EditText) findViewById(R.id.txtEmail); 
      TabHost tabHost = (TabHost) findViewById(R.id.tabHost); 

      tabHost.setup(); 

      TabHost.TabSpec tabSpec = tabHost.newTabSpec("Creator"); 
      tabSpec.setContent(R.id.tabCreator); 
      tabSpec.setIndicator("Creator"); 
      tabHost.addTab(tabSpec); 

      tabSpec = tabHost.newTabSpec("list"); 
      tabSpec.setContent(R.id.tabContact); 
      tabSpec.setIndicator("list"); 
      tabHost.addTab(tabSpec); 

      final Button addBtn = (Button) findViewById(R.id.bntEnviar); 
     } 


     @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; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 
      if (id == R.id.action_settings) { 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

} 

我在做什麼錯?

我的應用程序文件在調試文件下,只需滑動條。

+0

你可以上傳代碼嗎? – Kedarnath

+1

'MainActivity.java'中的行號35是什麼? – Hariharan

+1

你確定'tabHost'不是null嗎? –

回答

0

你有一個空指針異常,也就是說,你試圖使用一個你可能沒有初始化的對象。

如果您發佈您的活動課程,這將非常有幫助。

+1

它的帖子 – Mriaco

+0

該錯誤在第35行。 是否可以得到您爲tabHost獲得的值爲空? – Tal87