第一个程序导致错误

时间:2014-04-13 00:41:17

标签: java android eclipse genymotion

我刚开始学习Android编程并遇到麻烦。我正在使用这本书" Android编程The Big Nerd Ranch Guide"。我的IDE是Eclipse和Genymotion来模拟应用程序。这是logcat:

04-13 00:19:48.065: D/dalvikvm(1256): Late-enabling CheckJNI
04-13 00:19:48.781: D/AndroidRuntime(1256): Shutting down VM
04-13 00:19:48.785: W/dalvikvm(1256): threadid=1: thread exiting with uncaught exception (group=0xa4d8ab20)
04-13 00:19:48.785: E/AndroidRuntime(1256): FATAL EXCEPTION: main
04-13 00:19:48.785: E/AndroidRuntime(1256): Process: com.bignerdranch.android.geoquiz, PID: 1256
04-13 00:19:48.785: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.os.Looper.loop(Looper.java:136)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at java.lang.reflect.Method.invoke(Method.java:515)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at dalvik.system.NativeStart.main(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256):     at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.Activity.performCreate(Activity.java:5231)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-13 00:19:48.785: E/AndroidRuntime(1256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-13 00:19:48.785: E/AndroidRuntime(1256):     ... 11 more
04-13 00:19:56.345: I/Process(1256): Sending signal. PID: 1256 SIG: 9

这里是fragment_quiz.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>

QuizActivity.java文件(Eclipse导入的大部分代码):

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
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);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

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

            }
        });
    }

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

}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

将您的布局定义放在PlaceholderFragment类中。对于findViewById,请使用rootView.findViewById。对于上下文,请使用getActivity()

为什么这样做?实际上有两个独立的布局在屏幕中显示为一个。主人显示一切,包括片段。片段基本上是一个独立的迷你活动,在平板电脑开发中非常有用,即使只是用于手机类型的设备也很有用。您的布局位于fragment_quiz xml文件中,该文件仅在onCreateView语句中的片段内膨胀。只要你在findViewById膨胀之后使用它,你就可以了。