java空指针异常不打印栈_java - 应用程序空指针异常 - 堆栈内存溢出

我已经编写了这样的应用程序(它仍在进行中...)。 这是日志:

10-02 13:38:18.714: D/AndroidRuntime(637): Shutting down VM

10-02 13:38:18.714: W/dalvikvm(637): threadid=1: thread exiting with uncaught exception (group=0x40a13300)

10-02 13:38:18.734: E/AndroidRuntime(637): FATAL EXCEPTION: main

10-02 13:38:18.734: E/AndroidRuntime(637): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test1/com.example.test1.MainActivity}: java.lang.NullPointerException

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.ActivityThread.access$600(ActivityThread.java:130)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.os.Handler.dispatchMessage(Handler.java:99)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.os.Looper.loop(Looper.java:137)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.ActivityThread.main(ActivityThread.java:4745)

10-02 13:38:18.734: E/AndroidRuntime(637): at java.lang.reflect.Method.invokeNative(Native Method)

10-02 13:38:18.734: E/AndroidRuntime(637): at java.lang.reflect.Method.invoke(Method.java:511)

10-02 13:38:18.734: E/AndroidRuntime(637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

10-02 13:38:18.734: E/AndroidRuntime(637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

10-02 13:38:18.734: E/AndroidRuntime(637): at dalvik.system.NativeStart.main(Native Method)

10-02 13:38:18.734: E/AndroidRuntime(637): Caused by: java.lang.NullPointerException

10-02 13:38:18.734: E/AndroidRuntime(637): at com.example.test1.MainActivity.readFile(MainActivity.java:98)

10-02 13:38:18.734: E/AndroidRuntime(637): at com.example.test1.MainActivity.onCreate(MainActivity.java:52)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.Activity.performCreate(Activity.java:5008)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

10-02 13:38:18.734: E/AndroidRuntime(637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

10-02 13:38:18.734: E/AndroidRuntime(637): ... 11 more

10-02 13:38:21.984: I/Process(637): Sending signal. PID: 637 SIG: 9

我对此很陌生,但是我找不到错误所在。 上次我运行它时一切都很好。 我主要担心它是否正确读取文件。 代码下方:

public class MainActivity extends ActionBarActivity {

private int currentQuestion;

//private String [] questions2;

private String [] answers;

private Button answerButton;

private Button questionButton;

private TextView questionView;

private TextView answerView;

private EditText answerText;

private Question [] questions;

private Button buttonA;

private Button buttonB;

private Button buttonC;

private Button buttonD;

//private ContextWrapper context;

//AssetManager am = context.getAssets();

//InputStream is = am.open("questions.txt");

String fileName = "questions.txt";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

try {

questions=readFile(fileName);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

init();

}

Question[] readFile(String fileName) throws IOException {

String line, content,a,b,c,d,correct;

int id, x = 0;

StringTokenizer st = null;

BufferedReader br = null;

//opens the file to read

try {

br = new BufferedReader(new FileReader(fileName));

}

catch(FileNotFoundException fnfe) {

//JOptionPane.showMessageDialog(null,"There is NOT such a file!");

}

try {

if((line=br.readLine())==null){ // checks if the file is empty

// JOptionPane.showMessageDialog(null,"Data File is empty!");

}

else { // reads the file if not empty

//int size = Integer.parseInt(line);

//array = new Person[size];

}

while((line=br.readLine())!=null) {

st= new StringTokenizer(line, ",");

id = Integer.parseInt(st.nextToken());

content = st.nextToken();

a = st.nextToken();

b = st.nextToken();

c = st.nextToken();

d = st.nextToken();

correct = st.nextToken();

questions[x++] = new Question(id, content, a, b, c, d, correct);

}

}

catch(NullPointerException npe){}

br.close();

return questions;

}

// http://stackoverflow.com/questions/5771366/reading-a-simple-text-file

public void init()

{

//questions = new String[]{"What is the capital of Egypt?","What class are you in right now?"};

//answers = new String[]{"Cairo","IST380"};

//currentQuestion = -1;

// TextView ecie = (TextView)findViewById(R.id.QuestionTextView);

answerButton = (Button)findViewById(R.id.AnswerButton);

questionButton = (Button)findViewById(R.id.QuestionButton);

questionView = (TextView)findViewById(R.id.QuestionTextView);

answerView = (TextView) findViewById(R.id.AnswerTextView);

answerText = (EditText) findViewById(R.id.AnswerText);

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

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

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

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

//String pecie = questions[0].getContent();

//ecie.setText(pecie);

answerButton.setOnClickListener(new OnClickListener()

{

public void onClick(View v) {

checkAnswer();

}});

questionButton.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

showQuestion();

}});

}

/*

* This method * 1: increment currentQuestion index

* 2: check if it is equal to the size of the array and rest

if necessary

* 3: display the question at currentQuestion index in

question view

* 4: Empty answer view

*/

public void showQuestion()

{

currentQuestion++;

if(currentQuestion == questions.length)

currentQuestion =0;

questionView.setText(/*questions[currentQuestion]*/questions[currentQuestion].getContent());

answerView.setText("");

answerText.setText("");

}

/*

* This method return true if the answer equals to correct

answer

* (Ignoring case)

*/

public boolean isCorrect(String answer)

{

return (answer.equalsIgnoreCase(questions[currentQuestion].getCorrect()));

}

/* this method :

* 1: Read the text ( answer) from the answerTextEdit

* 2: call the isCorrect method

* 3: display the appropriate message.

*/

public void checkRight()

{

// String right

}

public void checkAnswer()

{

String answer = questions[currentQuestion].getCorrect();

if(isCorrect(answer))

answerView.setText("You're right!");

else

answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);

}

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

}

}

表现:

package="com.example.test1"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="21" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

我的值和styles.xml也有问题。 Maybye是由于事实,我在学校和家里工作,但仍然不知道问题出在哪里。 在styles.xml中,在

根据需要,activity_main文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.test1.MainActivity"

android:orientation="vertical"

android:background="#ff8400" >

android:id="@+id/QuestionTextView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:id="@+id/AnswerText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:layout_below="@+id/QuestionTextView"

android:ems="10" >

android:id="@+id/QuestionButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/AnswerText"

android:layout_below="@+id/AnswerText"

android:text="Show Next Question" />

android:id="@+id/AnswerButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/AnswerTextView"

android:layout_below="@+id/AnswerTextView"

android:layout_marginTop="18dp"

android:text="Check Answer" />

android:id="@+id/AnswerTextView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/QuestionButton"

android:layout_below="@+id/QuestionButton"

android:layout_marginTop="18dp"

android:ems="10" />

android:id="@+id/buttonA"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/AnswerButton"

android:layout_below="@+id/AnswerButton"

android:text="A" />

android:id="@+id/buttonB"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/AnswerButton"

android:layout_marginLeft="22dp"

android:layout_toRightOf="@+id/buttonA"

android:text="B" />

android:id="@+id/buttonC"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/buttonA"

android:layout_below="@+id/buttonA"

android:layout_marginTop="36dp"

android:text="C" />

android:id="@+id/buttonD"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBaseline="@+id/buttonC"

android:layout_alignBottom="@+id/buttonC"

android:layout_alignLeft="@+id/buttonB"

android:text="D" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值