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

时间:2014-02-12 07:17:32

标签: java android nullpointerexception

由于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

这是我的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>

2 个答案:

答案 0 :(得分:0)

“editText1”的值可能是Null导致崩溃。检查并修复它以便在xml和&amp ;;中正确包含它在代码中引用以删除崩溃

答案 1 :(得分:0)

我的布局中没有@+id/switch1; findViewById()将返回null。

UPD:您可能想阅读Re-using Layouts with <include/>