我很難理解此錯誤。我有一個在Main_Activitie選項菜單中啓動的inent。
我在我的LogCat中遇到此錯誤: 07-07 20:09:40.317:E/AndroidRuntime(2803):致命例外:主 07-07 20:09:40.317:E/AndroidRuntime(2803):java .lang.RuntimeException:無法實例化活動ComponentInfo {com.TS.3000/com.TS.3000.AddRestaurantActivity}:java.lang.NullPointerException
當我調試那裏說有NPE和無源找到窗口出現。我試過添加新的源文件夾,但不會在錯誤中更改。這裏是正在調用的意圖我的主要活動區域:從MainActivity選項菜單到第二個活動錯誤的意圖/ Android清單
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// this handles when user selects add button
Intent addRestaurant = new Intent(MainActivity.this,
AddRestaurantActivity.class);
Log.i("First", "Log.v");
startActivity(addRestaurant);
Log.i("Second", "Log.v");
return super.onOptionsItemSelected(item);
}
** * **AddRestaurantActivity* ** * **秒活動是叫做
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_restaurant);
Log.i("fourth", "Log.v");
restName = (EditText) findViewById(R.id.edtT_rest_name);
restStreet = (EditText) findViewById(R.id.edtT_restaurant_street_address);
restCity = (EditText) findViewById(R.id.edtT_restaurant_city_address);
phoneNumber = (EditText) findViewById(R.id.edtT_restaurant_phone);
Button saveRestaurant = (Button) findViewById(R.id.btn_saveRestaurant);
saveRestaurant.setOnClickListener(saveRestaurantButtonClicked);
}
OnClickListener saveRestaurantButtonClicked = new OnClickListener() {
String restaurantName = restName.getText().toString();
@Override
public void onClick(View v) {
if (restName.getText().length() != 0) {
Intent intent = new Intent(AddRestaurantActivity.this,
MainActivity.class);
intent.putExtra("restName", restaurantName);
startActivity(intent);
Log.i("fifth", "Log.v");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
AddRestaurantActivity.this);
builder.setTitle(R.string.errorTitle);
builder.setMessage(R.string.errorMessage);
builder.setPositiveButton(R.string.errorButton, null);
builder.show();
}
}
};
這裏是XML wh趁着我加的意圖:
<activity
android:name=".AddRestaurantActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
當我在MainActivity註釋掉我OnClickListner意圖的AddRestaurantActivity運行良好。
我不確定問題是在AddRestaurantActivity清單中聲明我的意圖,還是需要將源代碼添加到android.jar中。我是新來的android和調試這個問題是困難的。謝謝
謝謝,我只是試過,並沒有改變結果。感謝您的回覆 – user2533762
我想出了這一個。我沒有在主要活動中聲明一個onActivityResult。我不認爲這會導致問題,但當我加入主要活動時,一切運作良好。 – user2533762