我很难理解这个错误。我有一个从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和No Source Found窗口出现。我已经尝试添加新的源文件夹,但没有更改错误。以下是我的主要活动中调用意图的部分:
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:
<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>
当我注释掉我的OnClickListner时,从MainActivity到AddRestaurantActivity的意图运行正常。
我不确定问题是在AddRestaurantActivity的清单中声明我的意图,还是需要向android.jar添加源代码。我是android的新手,调试这个问题很困难。感谢
答案 0 :(得分:0)
主要活动和AddRestaurantActivity是否在同一个包中?您可以尝试将android:name =“。AddRestaurantActivity”更改为完整的包名,例如android:name =“com.myapplication.activity.AddRestaurantActivity”。