我是一名使用android的初學者,所以承擔我的知識缺乏 我正在開發一個項目,該項目一直工作正常,經過幾次小改動後,它開始給我一個RuntimeException unable to instantiate activity ComponentInfo{com.id11298775.exercise5/com.id11298775.excercise5.MainActivity}: java.lang.NullPointerException
。我已經恢復了適配器中的小改動,但仍然無法運行該應用程序。 這裏是我的目錄下載: RuntimeExpection無法實例化活動
我試圖弄清楚什麼是錯我的主要活動在相當長的時間,但對我來說似乎一切都很好,所以我想我失去了一些東西 這是我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'後才使用適配器。 – mikey