我刚开始使用我的第一个Android应用程序。我正在使用那本Big Nerd Ranch指南书,并在书中做了第一个例子。不确定我做错了什么。这是我的代码和错误详细信息。如果有人能提供帮助,我们将不胜感激。
package com.example.geoquiz2;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.Toast;
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_quiz, container,
false);
return rootView;
}
}
}
以下是出现的错误。
07-13 13:42:34.875: D/AndroidRuntime(1107): Shutting down VM
07-13 13:42:34.875: W/dalvikvm(1107): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-13 13:42:34.895: E/AndroidRuntime(1107): FATAL EXCEPTION: main
07-13 13:42:34.895: E/AndroidRuntime(1107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geoquiz2/com.example.geoquiz2.QuizActivity}: java.lang.NullPointerException
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.os.Looper.loop(Looper.java:137)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-13 13:42:34.895: E/AndroidRuntime(1107): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 13:42:34.895: E/AndroidRuntime(1107): at java.lang.reflect.Method.invoke(Method.java:525)
07-13 13:42:34.895: E/AndroidRuntime(1107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-13 13:42:34.895: E/AndroidRuntime(1107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-13 13:42:34.895: E/AndroidRuntime(1107): at dalvik.system.NativeStart.main(Native Method)
07-13 13:42:34.895: E/AndroidRuntime(1107): Caused by: java.lang.NullPointerException
07-13 13:42:34.895: E/AndroidRuntime(1107): at com.example.geoquiz2.QuizActivity.onCreate(QuizActivity.java:27)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.Activity.performCreate(Activity.java:5133)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-13 13:42:34.895: E/AndroidRuntime(1107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-13 13:42:34.895: E/AndroidRuntime(1107): ... 11 more
答案 0 :(得分:0)
你在第27行收到NullPointerException
。如果我是对的,第27行是
mTrueButton.setOnClickListener(new View.OnClickListener() {
请检查mTrueButton
是否为null
。我对Android做的并不多,但如果我没记错,findViewById
可以返回null
。如果是这样,你可以看看[这里]。(findViewByID returns null)