0

我很难理解这个错误。我有一个从 Main_Activiti 选项菜单开始的意图。
我在我的 LogCat 中收到此错误:07-07 20:09:40.317: E/AndroidRuntime(2803): FATAL EXCEPTION: main 07-07 20:09:40.317: E/AndroidRuntime(2803): java.lang.RuntimeException :无法实例化活动 ComponentInfo{com.TS.3000/com.TS.3000.AddRestaurantActivity}:java.lang.NullPointerException
当我调试时说有 NPE 并且没有找到源窗口出现。我尝试添加新的源文件夹,但没有错误更改。这是我的主要活动中调用意图的部分:

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新手,调试这个问题很困难。谢谢

4

1 回答 1

0

Main Activity 和 AddRestaurantActivity 是否在同一个包中?您可以尝试将 android:name=".AddRestaurantActivity" 更改为完整的包名称,例如 android:name="com.myapplication.activity.AddRestaurantActivity"。

于 2013-07-07T21:02:28.500 回答