RuntimeExpection无法实例化活动

时间:2014-03-30 11:34:11

标签: java android nullpointerexception

我是android的初学者,因此我缺乏知识 我正在开发一个工作正常的项目,经过一些小的改动,它开始给我一个RuntimeException unable to instantiate activity ComponentInfo{com.id11298775.exercise5/com.id11298775.excercise5.MainActivity}: java.lang.NullPointerException。我已经恢复了在适配器中的小变化,但仍然无法运行应用程序。 这是我的catLog: enter image description here

我试图弄清楚我的主要活动有多长时间出了什么问题,但对我来说一切似乎都很好,所以我想我错过了什么 这是我的MainActivity课程:

package com.id11298775.exercise5;

// import statements are here



public class MainActivity extends Activity {

private final String URI = getString(R.string.URI);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get the subject name
    EditText subjectName = (EditText) findViewById(R.id.activity_main_subjectname_et);
    // associate editText with the context menu
    subjectName.setOnCreateContextMenuListener(this);

}

/**
 * Inflate the menu this adds items to the action bar
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * code that executes according to the menu item that is clicked
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // click help
    case R.id.help:
        help();
        return true;
        // click exit
    case R.id.exit:
        finish();

    default:
        return super.onOptionsItemSelected(item);
    }
}

/**
 * when help is clicked this method will display a page related to the
 * subject number that is entered If no subject name is entered display the
 * general page of subjects of the handbook
 */
public void help() {
    EditText subjectNumber = (EditText) findViewById(R.id.activity_main_subjectnumber_et);
    String number = subjectNumber.getText().toString();
    String url = URI + number;

    Uri uriUrl = Uri.parse(url);
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);
}

/**
 * Inflate the context menu
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
    super.onCreateContextMenu(menu, v, menuInfo);
}

/**
 * code that executes according to the context menu item that is selected
 */
@Override
public boolean onContextItemSelected(MenuItem item) {
    EditText nameEd = (EditText) findViewById(R.id.activity_main_subjectname_et);
    switch (item.getItemId()) {
    // capitalize is selected
    case R.id.contextmenu_capitalize:
        String subjectName = nameEd.getText().toString();
        String upperCase = subjectName.toUpperCase();
        nameEd.setText(upperCase);
        return true;
        // clear is selected
    case R.id.contextmenu_clear:
        nameEd.setText("");
        return true;

    default:
        return super.onContextItemSelected(item);
    }
}

/**
 * methdo called when showList button is clicked
 * @param v
 */
public void showListButtonClicked(View v) {

    Intent intent = new Intent(MainActivity.this, SubjectList.class);
    startActivity(intent);
}

}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.id11298775.exercise5"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.id11298775.exercise5.MainActivity"
        android:label="@string/app_name" 
         android:theme="@android:style/Theme.Holo.Light">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.id11298775.exercise5.SubjectList"></activity>
</application>

</manifest>

有人热衷于帮助我吗? 感谢您分享您的知识..

1 个答案:

答案 0 :(得分:2)

这很可能是你的问题:

private final String URI = getString(R.string.URI);

保留声明,但将方法调用放在onCreate()方法中:

private String uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    uri = getString(R.string.URI);
}