我搜索过StackOverflow并搜索了错误,但没有一个建议的解决方案适合我。大多数问题出现在MainActivity中,但我的MainActivity工作正常。尝试启动其他活动时会出现此问题。这是堆栈跟踪:
07-24 19:12:14.136: E/AndroidRuntime(20372): FATAL EXCEPTION: main
07-24 19:12:14.136: E/AndroidRuntime(20372): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.news_app/com.example.news_app.android.ui.SettingsActivity}: java.lang.NullPointerException
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.os.Looper.loop(Looper.java:137)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-24 19:12:14.136: E/AndroidRuntime(20372): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 19:12:14.136: E/AndroidRuntime(20372): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 19:12:14.136: E/AndroidRuntime(20372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-24 19:12:14.136: E/AndroidRuntime(20372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-24 19:12:14.136: E/AndroidRuntime(20372): at dalvik.system.NativeStart.main(Native Method)
07-24 19:12:14.136: E/AndroidRuntime(20372): Caused by: java.lang.NullPointerException
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.Activity.findViewById(Activity.java:1882)
07-24 19:12:14.136: E/AndroidRuntime(20372): at com.example.news_app.android.ui.SettingsActivity.<init>(SettingsActivity.java:12)
07-24 19:12:14.136: E/AndroidRuntime(20372): at java.lang.Class.newInstanceImpl(Native Method)
07-24 19:12:14.136: E/AndroidRuntime(20372): at java.lang.Class.newInstance(Class.java:1319)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
07-24 19:12:14.136: E/AndroidRuntime(20372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
07-24 19:12:14.136: E/AndroidRuntime(20372): ... 11 more
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.news_app"
android:versionCode="1"
android:versionName="0.0.1-SNAPSHOT" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature android:name="android.hardware.wifi" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name=".android.ui.MainActivityNew" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".android.ui.NewPostActivity" >
</activity>
<activity android:name=".android.ui.PostDetailsActivity" >
</activity>
<activity android:name=".android.ui.SettingsActivity" >
</activity>
<activity android:name=".android.ui.UserDetailsActivity" >
</activity>
</application>
</manifest>
以下是我启动活动的MainActivity部分:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.action_new_post:
intent = new Intent(this, NewPostActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return false;
}
}
这里是SettingsActivity:
package com.example.news_app.android.ui;
import com.example.news_app.R;
import com.example.news_app.android.Settings;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class SettingsActivity extends Activity {
private Settings appSettings = new Settings(this);
private EditText editTextName = (EditText) findViewById(R.id.editTextName);
private EditText editTextAbout = (EditText) findViewById(R.id.editTextAbout);
private EditText editTextEmail = (EditText) findViewById(R.id.editTextEmail);
private EditText editTextPhone = (EditText) findViewById(R.id.editTextPhone);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
editTextName.setText(appSettings.getName());
editTextAbout.setText(appSettings.getAbout());
editTextEmail.setText(appSettings.getEmail());
editTextPhone.setText(appSettings.getPhone());
}
protected void onButtonSaveClick(View view) {
appSettings.setName(editTextName.getText().toString());
appSettings.setAbout(editTextAbout.getText().toString());
appSettings.setEmail(editTextEmail.getText().toString());
appSettings.setPhone(editTextPhone.getText().toString());
this.finish();
}
}
感谢您的帮助!
答案 0 :(得分:3)
Ok这是您的问题,您正在查找视图而不会使布局膨胀
private EditText editTextName = (EditText) findViewById(R.id.editTextName);
private EditText editTextAbout = (EditText) findViewById(R.id.editTextAbout);
private EditText editTextEmail = (EditText) findViewById(R.id.editTextEmail);
private EditText editTextPhone = (EditText) findViewById(R.id.editTextPhone);
在oncreate中使用setContentView
后应声明这些声明。你可以这样做来实现这个目标;声明像这样的视图
private EditText editTextAbout;
private EditText editTextEmail;
private EditText editTextPhone;
然后在调用setContentView之后在onCreate中添加这些
editTextAbout = (EditText) findViewById(R.id.editTextAbout);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPhone = (EditText) findViewById(R.id.editTextPhone);