由于共享的首选项,应用程序在启动时崩溃



应用程序现在由于nullpointerexception而崩溃。我在谷歌上发现了许多不同的类似问题,然而,没有一个解决方案奏效。这个应用程序应该转发一个呼叫,其中接收转发呼叫的号码存储在共享的首选项中。至于要转发的数字,则存储在数据库中。如果可能的话,请指导我如何从数据库中获取数据。感谢

这些是logcats:

02-12 15:09:07.745: E/AndroidRuntime(4827): Caused by: java.lang.NullPointerException
02-12 15:09:07.745: E/AndroidRuntime(4827):     at com.example.awkwardpanda_redirectcall.MainActivity.onCreate(MainActivity.java:33)
02-12 15:09:07.745: E/AndroidRuntime(4827):     at android.app.Activity.performCreate(Activity.java:5158)
02-12 15:09:07.745: E/AndroidRuntime(4827):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-12 15:09:07.745: E/AndroidRuntime(4827):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
02-12 15:09:07.745: E/AndroidRuntime(4827):     ... 11 more

这是我的主要活动.java

public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch switch1 = (Switch) findViewById(R.id.switch1);
EditText editText1 = (EditText) findViewById(R.id.editText1);
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(this);
final String streditText1 = preferences.getString("Number", "");
editText1.setText(streditText1);

// set the switch to OFF
switch1.setChecked(false);
// attach a listener to check for changes in state
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Call Forwarding is activated",Toast.LENGTH_SHORT).show();
callforward("*63*" + streditText1 + "#"); // 0123456789 is the number you want to forward the calls.; 
}
else{
Toast.makeText(getApplicationContext(), "Call Forwarding is deactivated",Toast.LENGTH_SHORT).show();
callforward("#81#");
}
}
});}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void manage_numbers_onClick(View view) {
Intent myIntent = new Intent(this, Manage_numbers.class);
startActivity(myIntent);
} 
private void callforward(String ArrayString)
{
//DBAdapter db = new DBAdapter(this);
//db.getAllContacts().toString();
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri mmiCode = Uri.fromParts("tel", ArrayString, "#");
intentCallForward.setData(mmiCode);
startActivity(intentCallForward);
}
private class PhoneCallListener extends PhoneStateListener 
{
private boolean isPhoneCalling = false;        
@Override
public void onCallStateChanged(int state, String incomingNumber) 
{
if (TelephonyManager.CALL_STATE_RINGING == state)
{
// phone ringing
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) 
{
// active
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) 
{
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
if (isPhoneCalling)
{
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}

这是我的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left" 
android:text="@string/checktoconfirm"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:inputType="phone"
android:text="@string/receivenumbertext" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginTop="10dp"
android:text="@string/savebtn" />
</LinearLayout>

我在您的布局中没有看到@+id/switch1findViewById()将返回null。

UPD:您可能想阅读使用<包括/>

可能是"editText1"的值为Null导致了崩溃。检查并修复它以将其正确地包含在xml&在代码中引用以删除崩溃

最新更新