English 中文(简体)
Anders program problem, Null PointerExc
原标题:Android program problem, NullPointerExc

我的节目使我成为无一例外,我不知道为什么? 我知道,这与“减半”变量有关,但我不知道什么? 我能做些什么来解决这个问题?

清单:

03-18 16:14:55.852: ERROR/AndroidRuntime(277): FATAL EXCEPTION: main
03-18 16:14:55.852: ERROR/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.games.think/com.games.think.Think}: java.lang.NullPointerException
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.os.Looper.loop(Looper.java:123)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at java.lang.reflect.Method.invoke(Method.java:521)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at dalvik.system.NativeStart.main(Native Method)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): Caused by: java.lang.NullPointerException
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at com.games.think.Think.onCreate(Think.java:38)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-18 16:14:55.852: ERROR/AndroidRuntime(277):     ... 11 more

我的一些法典是:

     package com.games.think;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class Think extends Activity implements OnClickListener{

    int question = 1, lvl;

    /** Called when the activity is first created. */
    RadioButton lvl1;
    RadioButton lvl2;
    RadioButton lvl3;
    RadioButton lvl4;
    RadioButton lvl5;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button play = (Button)findViewById(R.id.play);
        play.setOnClickListener(this);
        Button level = (Button)findViewById(R.id.level);
        level.setOnClickListener(this);
        Button exit = (Button)findViewById(R.id.exit);
        exit.setOnClickListener(this);
        Button setLevel = (Button)findViewById(R.id.setLevel);
        setLevel.setOnClickListener(this);
        lvl1 = (RadioButton)findViewById(R.id.lvl1);
        lvl2 = (RadioButton)findViewById(R.id.lvl2);
        lvl3 = (RadioButton)findViewById(R.id.lvl3);
        lvl4 = (RadioButton)findViewById(R.id.lvl4);
        lvl5 = (RadioButton)findViewById(R.id.lvl5);

        lvl = getLevel();
        if(lvl == -1) {
            lvl=getLevel();
        }


    }

    @SuppressWarnings("null")
    private int getLevel() {

        String FILENAME = "think_level";
        FileInputStream fis;
        byte[] buffer = new byte[1000];



        try {
            fis = openFileInput(FILENAME);
        } catch (FileNotFoundException e) {
            setLevel("1");
            return -1;
        }

        try {
            fis.read(buffer,0,1000);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String level = buffer.toString();
        int iLevel = Integer.valueOf(level);
        return iLevel;
    }

    private void setLevel(String level) {
        String FILENAME = "think_level";
        String string = level;

        FileOutputStream fos;
        try {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(string.getBytes());
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }

    @Override
    public void onClick(View v) {
        switch( v.getId()){
        case R.id.play:
            setContentView(R.layout.play);
            setQuestion();
        case R.id.level:
            setContentView(R.layout.level);
            switch(getLevel()) {
            case 1:
                lvl1.setChecked(true);
            case 2:
                lvl2.setChecked(true);
            case 3:
                lvl3.setChecked(true);
            case 4:
                lvl4.setChecked(true);
            case 5:
                lvl5.setChecked(true);

            }

        case R.id.setLevel:
            if(lvl1.isChecked()) {
                setLevel("1");
            }
            if(lvl2.isChecked()) {
                setLevel("2");
            }
            if(lvl3.isChecked()) {
                setLevel("3");
            }
            if(lvl4.isChecked()) {
                setLevel("4");
            }
            if(lvl5.isChecked()) {
                setLevel("5");
            }







        }
    }

    private void setQuestion() {




    }



    }
最佳回答

如果带宽的“固定水平”的 but子位于主轴。 如果是其他地方的话,你就不能这样认为:

 Button setLevel = (Button)findViewById(R.id.setLevel);

But you need an inflater.

问题回答

如果能够理解,则有37-38条线

Button setLevel = (Button)findViewById(R.id.setLevel);
        setLevel.setOnClickListener(this);

见<条码>等。 附有<代码>的年份 iii

Button setLevel = (Button)findViewById(R.id.setLevel);

该行不归还物体。 确保你有id权,并有一个县登记。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签