我确定,我错过了一些非常简单的东西,但由于某种原因,运行这段代码后我得到Null指针异常。有人知道为什么吗?
11-27 20:27:30.568: E/AndroidRuntime(32479): FATAL EXCEPTION: main
11-27 20:27:30.568: E/AndroidRuntime(32479): Process: com.example.makemyday, PID: 32479
11-27 20:27:30.568: E/AndroidRuntime(32479): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.makemyday/com.example.makemyday.ActionDetailActivity}: java.lang.NullPointerException
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.os.Looper.loop(Looper.java:137)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-27 20:27:30.568: E/AndroidRuntime(32479): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 20:27:30.568: E/AndroidRuntime(32479): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 20:27:30.568: E/AndroidRuntime(32479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-27 20:27:30.568: E/AndroidRuntime(32479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-27 20:27:30.568: E/AndroidRuntime(32479): at dalvik.system.NativeStart.main(Native Method)
11-27 20:27:30.568: E/AndroidRuntime(32479): Caused by: java.lang.NullPointerException
11-27 20:27:30.568: E/AndroidRuntime(32479): at com.example.makemyday.EventDetailsFragment.onCreateView(EventDetailsFragment.java:34)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-27 20:27:30.568: E/AndroidRuntime(32479): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
fragment_event_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="132dp" >
<EditText
android:id="@+id/event_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint_description"
android:inputType="text"
android:padding="16dp"
android:textIsSelectable="true"
android:scrollbars = "vertical"
android:lines="4"
android:textSize="14sp" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="0dip"
android:paddingRight="0dip" >
<EditText
android:id="@+id/quantity"
android:layout_width="166dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:textIsSelectable="true"
/>
<Spinner
android:id="@+id/category_quantity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:textSize="14dip" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="0dip"
android:paddingRight="0dip" >
<Spinner
android:id="@+id/category_query"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:textSize="14dip" />
</LinearLayout>
</LinearLayout>
EventDetailsFragment
package com.example.makemyday;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class EventDetailsFragment extends Fragment implements OnItemSelectedListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_action_detail,
container, false);
// Show the dummy content as text in a TextView.
Spinner category=(Spinner)rootView.findViewById(R.id.category_query);
ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this.getActivity().getBaseContext(),R.array.category_names, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapterCategory);
category.setOnItemSelectedListener(this);
Spinner quantity=(Spinner)rootView.findViewById(R.id.category_quantity);
ArrayAdapter<CharSequence> adapterQuantity = ArrayAdapter.createFromResource(this.getActivity().getBaseContext(),R.array.quantity_names, android.R.layout.simple_spinner_item);
adapterQuantity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
quantity.setAdapter(adapterQuantity);
quantity.setOnItemSelectedListener(this);
EditText description = (EditText)rootView.findViewById(R.id.event_description);
description.setText("this");
return rootView;
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:3)
您展示了名为fragment_event_detail.xml
的布局,但您夸大了名为R.layout.fragment_action_detail
的布局。检查onCreate()
View rootView = inflater.inflate(R.layout.fragment_action_detail,
container, false);
替换下面的布局,
View rootView = inflater.inflate(R.layout.fragment_event_detail,
container, false);