我的代码上出现空指针异常

时间:2014-10-12 02:53:41

标签: java android android-asynctask

我设置并完成了所有代码。我尝试运行我的应用程序,以确保它按预期运行,我得到不幸的是,您的应用已停止。我试着看看这个问题的其他例子,但无法找出我的应用程序究竟出了什么问题。我检查了我的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);
    }
}

2 个答案:

答案 0 :(得分:0)

您无法在类定义中使用findViewById。没有定义视图。所有findViewById()次调用都属于setContentView()onCreate()之后的

改变这个:

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等。

答案 1 :(得分:-2)

它正在寻找ComponentInfo活动而你的代码似乎只有一个名为MainActivity的代码?您可能需要更新清单以指向MainActivity