gpt4 book ai didi

java - RuntimeExpection 无法实例化 Activity

转载 作者:行者123 更新时间:2023-12-02 06:00:07 26 4
gpt4 key购买 nike

我是android初学者,所以请忍受我的知识匮乏我正在开发一个项目,该项目一直运行良好,但经过一些小的更改后,它开始给我一个 RuntimeException 无法实例化 Activity ComponentInfo{com.id11298775.exercise5/com.id11298775.excercise5.MainActivity}: java. lang.NullPointerException。我已经恢复了适配器中的小更改,但我仍然无法运行该应用程序。这是我的猫日志: enter image description here

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

}

这是我的 list :

<?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>

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

最佳答案

这很可能是您的问题:

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

关于java - RuntimeExpection 无法实例化 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22743476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com