我是Android编程新手。我使用android book学习这段代码。 本书没有fragemnet布局文件夹,我有一个这个文件夹.. 所以我不知道这个问题。 我只需添加两个按钮并在手机上运行。 我正在尝试制作一个setOnClickListener方法,app不会运行。
请帮帮我
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);
mFalseButton = (Button)findViewById(R.id.false_button);
添加方法
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT)
.show();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.incorrect_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;
}
}
}
log cat
03-15 19:51:36.727: E/Trace(2843): error opening trace file: No such file or directory (2)
03-15 19:51:36.727: D/ActivityThread(2843): setTargetHeapUtilization:0.25
03-15 19:51:36.727: D/ActivityThread(2843): setTargetHeapIdealFree:8388608
03-15 19:51:36.727: D/ActivityThread(2843): setTargetHeapConcurrentStart:2097152
03-15 19:51:36.747: V/ActivityThread(2843): Class path: /data/app/com.bignerdranch.geoquiz-1.apk, JNI path: /data/data/com.bignerdranch.geoquiz/lib
03-15 19:51:36.837: I/ActionBarImpl(2843): mActivity.getLocalClassName : QuizActivityid : 0
03-15 19:51:36.837: I/ActionBarImpl(2843): mContext.getPackageName : com.bignerdranch.geoquizid : 0
03-15 19:51:36.837: W/dalvikvm(2843): threadid=1: thread exiting with uncaught exception (group=0x4121a438)
03-15 19:51:36.847: E/AndroidRuntime(2843): FATAL EXCEPTION: main
03-15 19:51:36.847: E/AndroidRuntime(2843): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.geoquiz/com.bignerdranch.geoquiz.QuizActivity}: java.lang.NullPointerException
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.ActivityThread.access$600(ActivityThread.java:138)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.os.Looper.loop(Looper.java:137)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.ActivityThread.main(ActivityThread.java:4929)
03-15 19:51:36.847: E/AndroidRuntime(2843): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 19:51:36.847: E/AndroidRuntime(2843): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 19:51:36.847: E/AndroidRuntime(2843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-15 19:51:36.847: E/AndroidRuntime(2843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-15 19:51:36.847: E/AndroidRuntime(2843): at dalvik.system.NativeStart.main(Native Method)
03-15 19:51:36.847: E/AndroidRuntime(2843): Caused by: java.lang.NullPointerException
03-15 19:51:36.847: E/AndroidRuntime(2843): at com.bignerdranch.geoquiz.QuizActivity.onCreate(QuizActivity.java:29)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.Activity.performCreate(Activity.java:5238)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
03-15 19:51:36.847: E/AndroidRuntime(2843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
03-15 19:51:36.847: E/AndroidRuntime(2843): ... 11 more
fragment_xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
/>
</LinearLayout>
</LinearLayout>
active_xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bignerdranch.geoquiz.QuizActivity"
tools:ignore="MergeRootFrame" />