【问题标题】:RuntimeExpection unable to instantiate activityRuntimeExpection 无法实例化活动
【发布时间】:2014-05-09 17:22:15
【问题描述】:

我是android的初学者,所以请忍受我的知识不足 我正在开展一个项目,该项目一直运行良好,经过一些小改动后,它开始给我RuntimeException unable to instantiate activity ComponentInfo{com.id11298775.exercise5/com.id11298775.excercise5.MainActivity}: java.lang.NullPointerException。我已经恢复了适配器中的小更改,但我仍然无法运行该应用程序。 这是我的猫日志:

很长一段时间以来,我一直试图弄清楚我的主要活动出了什么问题,但对我来说一切似乎都很好,所以我想我错过了一些东西 这是我的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>

有人愿意帮助我吗? 感谢您分享您的知识..

【问题讨论】:

  • 我有一个适配器,我想访问适配器中的 strings.xml 文件。所以我发现我需要在适配器中将上下文作为参数传递,我已经做到了。然后应用程序开始崩溃,所以我恢复了更改。该应用程序仍然没有从一开始就运行。我只在按下按钮表单Main_Activity 后使用适配器。

标签: java android nullpointerexception


【解决方案1】:

这很可能是您的问题:

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);
}

【讨论】:

  • 你是对的!这是工作!知道为什么有一段时间它起作用了,我在那里声明了 URI 变量 4/5 天然后它就停止了?
  • @mikey 应该没用,不知道 :) 请接受答案。
  • 哦,好吧,无论如何,我会尽快接受答案
  • 所以错误是由getString()方法引起的,也就是说我不能在创建MainActivity之前调用它,对吗?
  • 我的猜测是 Context,即您的 Activity 尚未初始化,因为在您初始化 String 时尚未调用 onCreate() 方法。还要检查这个问题:stackoverflow.com/questions/4253328/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多