簡體   English   中英

由於共享的首選項,應用在啟動時崩潰

[英]App crashes on start up due to shared preferences

由於nullpointerexception,應用程序現在崩潰。 我在Google上發現了許多不同的類似問題,但是,沒有一種解決方案有效。 該應用程序應該轉接一個呼叫,其中將接收轉接呼叫的號碼存儲在共享首選項中。 至於要轉發的號碼都存儲在數據庫中。 如果可能,請也指導我如何從數據庫中獲取數據。 謝謝

這些是日志貓:

 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

這是我的mainactivity.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>

可能“ editText1”的值為Null,這會導致崩潰。 檢查並修復它,使其正確包含在xml中,並在代碼中引用以消除崩潰

我在您的布局中沒有看到@+id/switch1 findViewById()將返回null。

UPD:您可能想閱讀<include />的“重用布局”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM