2014-10-12 121 views
-3

我已完成所有代碼的設置並完成。我試着運行我的應用程序,以確保它的功能,因爲它應該和我得到一個不幸的是,您的應用程序已停止。我試着看看這個問題的其他例子,但無法找出我的應用程序實際出現了什麼問題。我檢查了我的LogCat,仍然無法弄清楚。如果我能得到我應該看的東西,我會很感激。我的代碼爲空指針異常

 
10-11 22:13:01.521: W/AudioService(286): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg 
10-11 22:13:01.540: W/AudioService(286): onLoadSoundEffects(), Error -1 while loading samples 
10-11 22:13:01.551: I/ActivityManager(286): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=net.androidbootcamp.zipcarrentalapp/.MainActivity} from pid 414 
10-11 22:13:01.611: W/WindowManager(286): Screenshot failure taking screenshot for (266x425) to layer 21010 
10-11 22:13:01.691: W/dalvikvm(859): PR_CAPBSET_DROP 0 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled. 
10-11 22:13:01.771: D/dalvikvm(859): Not late-enabling CheckJNI (already on) 
10-11 22:13:01.781: I/ActivityManager(286): Start proc net.androidbootcamp.zipcarrentalapp for activity net.androidbootcamp.zipcarrentalapp/.MainActivity: pid=859 uid=10046 gids={50046, 1028} 
10-11 22:13:03.620: D/AndroidRuntime(859): Shutting down VM 
10-11 22:13:03.650: W/dalvikvm(859): threadid=1: thread exiting with uncaught exception (group=0x41465700) 
10-11 22:13:03.770: E/AndroidRuntime(859): FATAL EXCEPTION: main 
10-11 22:13:03.770: E/AndroidRuntime(859): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.androidbootcamp.zipcarrentalapp/net.androidbootcamp.zipcarrentalapp.MainActivity}: java.lang.NullPointerException 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.os.Looper.loop(Looper.java:137) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-11 22:13:03.770: E/AndroidRuntime(859): at java.lang.reflect.Method.invokeNative(Native Method) 
10-11 22:13:03.770: E/AndroidRuntime(859): at java.lang.reflect.Method.invoke(Method.java:525) 
10-11 22:13:03.770: E/AndroidRuntime(859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-11 22:13:03.770: E/AndroidRuntime(859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-11 22:13:03.770: E/AndroidRuntime(859): at dalvik.system.NativeStart.main(Native Method) 
10-11 22:13:03.770: E/AndroidRuntime(859): Caused by: java.lang.NullPointerException 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.Activity.findViewById(Activity.java:1853) 
10-11 22:13:03.770: E/AndroidRuntime(859): at net.androidbootcamp.zipcarrentalapp.MainActivity.(MainActivity.java:16) 
10-11 22:13:03.770: E/AndroidRuntime(859): at java.lang.Class.newInstanceImpl(Native Method) 
10-11 22:13:03.770: E/AndroidRuntime(859): at java.lang.Class.newInstance(Class.java:1130) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 
10-11 22:13:03.770: E/AndroidRuntime(859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 
10-11 22:13:03.770: E/AndroidRuntime(859): ... 11 more 
10-11 22:13:04.510: W/ActivityManager(286): Force finishing activity net.androidbootcamp.zipcarrentalapp/.MainActivity 
10-11 22:13:04.983: I/WindowManager(286): Screenshot max retries 4 of Token{41ad2f58 ActivityRecord{41989b98 u0 net.androidbootcamp.zipcarrentalapp/.MainActivity}} appWin=Window{41ab4b70 u0 Starting net.androidbootcamp.zipcarrentalapp} drawState=4 
10-11 22:13:04.983: W/WindowManager(286): Screenshot failure taking screenshot for (800x1280) to layer 21015 
10-11 22:13:05.010: W/ActivityManager(286): Force finishing activity com.android.customlocale2/.CustomLocaleActivity 
10-11 22:13:05.450: I/Choreographer(414): Skipped 423 frames! The application may be doing too much work on its main thread. 
10-11 22:13:05.720: W/ActivityManager(286): Activity pause timeout for ActivityRecord{41989b98 u0 net.androidbootcamp.zipcarrentalapp/.MainActivity} 
10-11 22:13:05.970: I/Choreographer(286): Skipped 33 frames! The application may be doing too much work on its main thread. 
10-11 22:13:06.410: I/Choreographer(414): Skipped 32 frames! The application may be doing too much work on its main thread. 
10-11 22:13:06.780: I/ActivityManager(286): Activity reported stop, but no longer stopping: ActivityRecord{419b5e78 u0 com.android.launcher/com.android.launcher2.Launcher} 
10-11 22:13:08.680: E/SoundPool(286): error loading /system/media/audio/ui/Effect_Tick.ogg 
10-11 22:13:08.680: W/AudioService(286): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
10-11 22:13:08.871: E/SoundPool(286): error loading /system/media/audio/ui/KeypressStandard.ogg 
10-11 22:13:08.871: W/AudioService(286): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg 
10-11 22:13:08.911: E/SoundPool(286): error loading /system/media/audio/ui/KeypressSpacebar.ogg 
10-11 22:13:08.911: W/AudioService(286): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg 
10-11 22:13:08.931: E/SoundPool(286): error loading /system/media/audio/ui/KeypressDelete.ogg 
10-11 22:13:08.971: W/AudioService(286): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg 
10-11 22:13:08.971: E/SoundPool(286): error loading /system/media/audio/ui/KeypressReturn.ogg 
10-11 22:13:08.990: W/AudioService(286): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg 
10-11 22:13:09.001: W/AudioService(286): onLoadSoundEffects(), Error -1 while loading samples 
10-11 22:13:09.052: I/Process(859): Sending signal. PID: 859 SIG: 9 
10-11 22:13:09.521: I/ActivityManager(286): Process net.androidbootcamp.zipcarrentalapp (pid 859) has died. 
10-11 22:13:10.610: I/Choreographer(839): Skipped 603 frames! The application may be doing too much work on its main thread. 
package net.androidbootcamp.zipcarrentalapp; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity 
{ 
    private RadioGroup radioGroupId; 
    private RadioButton radioButton; 
    private Button button; 
     final RadioButton Compact = (RadioButton)findViewById(R.id.radCompact); 
     final RadioButton MidSize = (RadioButton)findViewById(R.id.radMidSize); 
     final RadioButton Luxury = (RadioButton)findViewById(R.id.radLuxury); 
     final TextView txtResult = (TextView) findViewById(R.id.txtResult); 
     double radCompact = 59.99; 
     double radMidSize = 65.99; 
     double radLuxury = 89.99; 
     int days = 0; 
     double computeValue; 

     public void operation(int days) 
     { 
      if(days <= 10) 
      { 
       if(Compact.isChecked()) 
       { 
        computeValue = radCompact * days; 
        txtResult.setText("Cost:" + Double.toString(computeValue)); 

       } 

       else if (MidSize.isChecked()) 
       { 
        computeValue = radMidSize * days; 
        txtResult.setText("Cost: " + Double.toString(computeValue)); 
       } 
       else if (Luxury.isChecked()) 
       { 
        computeValue = radLuxury * days; 
        txtResult.setText("Cost:" + Double.toString(computeValue)); 
       } 
      } 
     } 

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


    @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

發佈代碼... – Confuse 2014-10-12 02:59:16

+0

我懷疑你的問題是類似於這個: http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – 2014-10-12 03:07:37

+0

我不知道爲什麼你得到一個空指針異常。長相代碼看起來不錯...我的問題早with'Double.toString(...)'所以一旦嘗試做這一點 - '將String.valueOf(computeValue);' – Confuse 2014-10-12 03:11:08

回答

0

你不能在類定義中使用findViewById。沒有定義視圖。所有這些findViewById()電話屬於您的onCreate()setContentView()

更改此:

final RadioButton Compact = (RadioButton)findViewById(R.id.radCompact); 
final RadioButton MidSize = (RadioButton)findViewById(R.id.radMidSize); 
final RadioButton Luxury = (RadioButton)findViewById(R.id.radLuxury); 
final TextView txtResult = (TextView) findViewById(R.id.txtResult); 

RadioButton Compact; 
RadioButton MidSize; 
RadioButton Luxury; 
TextView txtResult; 

,改變

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Compact = (RadioButton) findViewById(R.id.radCompact); 
    MidSize = (RadioButton) findViewById(R.id.radMidSize); 
    Luxury = (RadioButton) findViewById(R.id.radLuxury); 
    txtResult = (TextView) findViewById(R.id.txtResult); 
} 

作爲風格和慣例,你應該開始用小寫字母的成員名稱,如Compact最好是rbCompact

-2

其尋找ComponentInfo活動,以及代碼只好像有一個叫MainActivity?你可能需要更新清單指向MainActivity