我试图在ADT中运行以下代码,每当我尝试将其作为Android应用程序运行时,我都会遇到标题中所述的错误。
这是.java文件中的代码。
package sp.com;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RestaurantList extends ActionBarActivity {
private EditText name;
private RadioGroup restaurantTypes;
private Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.name);
restaurantTypes = (RadioGroup) findViewById(R.id.types);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
}
private View.OnClickListener onSave = new View.OnClickListener() {
@Override
public void onClick(View v) {
String nameStr = name.getText().toString();
String restType = "";
switch (restaurantTypes.getCheckedRadioButtonId()){
case R.id.chinese:
restType = "Chinese";
break;
case R.id.indian:
restType = "Indian";
break;
case R.id.western:
restType = "Western";
break;
case R.id.indonesian:
restType = "Indonesian";
break;
case R.id.korean:
restType = "Korean";
break;
case R.id.japanese:
restType = "Japanese";
break;
case R.id.thai:
restType = "Thai";
break;
}
String combineStr = nameStr + "\n" + restType;
Toast.makeText(v.getContext(), combineStr, Toast.LENGTH_LONG).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.restaurant_list, menu);
return true;
}
}
这是fragment_restaurant_list.xml文件。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/TableLayout1"
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"
android:stretchColumns="1"
tools:context="sp.com.RestaurantList$PlaceholderFragment" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : " />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add :" />
<EditText
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tel :" />
<EditText
android:id="@+id/telephone "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/types"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/chinese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Chinese" />
<RadioButton
android:id="@+id/indian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indian" />
<RadioButton
android:id="@+id/western"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Western" />
<RadioButton
android:id="@+id/indonesian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indonesian" />
<RadioButton
android:id="@+id/korean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Korean" />
<RadioButton
android:id="@+id/japanese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Japanese" />
<RadioButton
android:id="@+id/thai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Thai" />
</RadioGroup>
</TableRow>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</TableLayout>
</ScrollView>
这是manifest.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sp.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sp.com.RestaurantList"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是logcat:
05-03 14:40:33.382: E/Zygote(32): setreuid() failed. errno: 2
05-03 14:40:45.852: E/Zygote(32): setreuid() failed. errno: 17
05-03 14:40:47.522: E/BatteryService(60): usbOnlinePath not found
05-03 14:40:47.522: E/BatteryService(60): batteryVoltagePath not found
05-03 14:40:47.522: E/BatteryService(60): batteryTemperaturePath not found
05-03 14:40:47.542: E/SurfaceFlinger(60): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
05-03 14:40:47.642: E/SensorService(60): couldn't open device for module sensors (Invalid argument)
05-03 14:40:54.203: E/System(60): Failure starting core service
05-03 14:40:54.203: E/System(60): java.lang.SecurityException
05-03 14:40:54.203: E/System(60): at android.os.BinderProxy.transact(Native Method)
05-03 14:40:54.203: E/System(60): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
05-03 14:40:54.203: E/System(60): at android.os.ServiceManager.addService(ServiceManager.java:72)
05-03 14:40:54.203: E/System(60): at com.android.server.ServerThread.run(SystemServer.java:206)
05-03 14:40:54.233: E/EventHub(60): could not get driver version for /dev/input/mouse0, Not a typewriter
05-03 14:40:54.233: E/EventHub(60): could not get driver version for /dev/input/mice, Not a typewriter
05-03 14:40:54.873: E/SoundPool(60): error loading /system/media/audio/ui/Effect_Tick.ogg
05-03 14:40:54.893: E/SoundPool(60): error loading /system/media/audio/ui/KeypressStandard.ogg
05-03 14:40:54.913: E/SoundPool(60): error loading /system/media/audio/ui/KeypressSpacebar.ogg
05-03 14:40:54.933: E/SoundPool(60): error loading /system/media/audio/ui/KeypressDelete.ogg
05-03 14:40:54.933: E/SoundPool(60): error loading /system/media/audio/ui/KeypressReturn.ogg
05-03 14:40:54.993: E/UsbObserver(60): java.lang.NullPointerException
05-03 14:40:54.993: E/UsbObserver(60): at com.android.server.UsbObserver.init(UsbObserver.java:131)
05-03 14:40:54.993: E/UsbObserver(60): at com.android.server.UsbObserver.<init>(UsbObserver.java:65)
05-03 14:40:54.993: E/UsbObserver(60): at com.android.server.ServerThread.run(SystemServer.java:402)
05-03 14:40:55.663: E/ThrottleService(60): Could not open GPS configuration file /etc/gps.conf
05-03 14:40:56.363: E/logwrapper(136): executing /system/bin/tc failed: No such file or directory
05-03 14:40:56.453: E/logwrapper(138): executing /system/bin/tc failed: No such file or directory
05-03 14:40:56.473: E/logwrapper(140): executing /system/bin/tc failed: No such file or directory
05-03 14:41:39.030: E/AndroidRuntime(336): FATAL EXCEPTION: main
05-03 14:41:39.030: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{sp.com/sp.com.RestaurantList}: java.lang.NullPointerException
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-03 14:41:39.030: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 14:41:39.030: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
05-03 14:41:39.030: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-03 14:41:39.030: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-03 14:41:39.030: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
05-03 14:41:39.030: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException
05-03 14:41:39.030: E/AndroidRuntime(336): at sp.com.RestaurantList.onCreate(RestaurantList.java:30)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 14:41:39.030: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-03 14:41:39.030: E/AndroidRuntime(336): ... 11 more
: E/(): Device disconnected: 1
: E/(): Device disconnected
非常感谢你,因为我是初学者,如果使用的术语很简单,我将不胜感激。
答案 0 :(得分:2)
您在错误的布局xml中查找视图,因为发布的布局名称为fragment_restaurant_list.xml
,但您将其设置为内容视图:
setContentView(R.layout.main);