为什么我得到空指针异常?

时间:2013-08-14 05:11:37

标签: android android-intent

我想返回一个布尔值来删除此代码中的按钮。我尝试从PassworConfirmationActivity获取布尔值,并尝试在IndexActivity中使用它,但我得到一个NullPointerException。

这是我得到NullPointerException的代码。

public class PasswordConfirmationActivity extends Activity {

Button ButtonClick;
EditText EdtText;
Boolean control =new Boolean(false);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_password_confirmation);
    EdtText = (EditText) findViewById(R.id.password);
    ButtonClick = (Button) findViewById(R.id.devam);
    ButtonClick.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent inte = new Intent(PasswordConfirmationActivity.this,
                    IndexActivity.class);
            control = true;
            inte.putExtra("confirmation", control);
            startActivity(inte);
            finish();
        }
    });
}

我正在使用意图启动

  

IndexActivity.class

当它尝试启动它时,给我nullpointerexception

这是IndexActivity:

public class IndexActivity extends Activity {
TextView txt1, txt2, txt3;
Button ButtonClick, ButtonClick2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GeneralConstans GC = new GeneralConstans();

    txt1 = (TextView) findViewById(R.id.textView1);
    txt2 = (TextView) findViewById(R.id.textView2);
    txt3 = (TextView) findViewById(R.id.textView5);

    ButtonClick = (Button) findViewById(R.id.order);
    ButtonClick.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, 0);
        }
    });

    ButtonClick2 = (Button) findViewById(R.id.login);
    ButtonClick2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent inte = new Intent(IndexActivity.this,
                    LoginActivity.class);

            startActivity(inte);
        }
    });
    ButtonClick2.setVisibility(1);
  

在这一行中,我从其他活动中获取参数“确认”。

        Boolean loginControl  = getIntent().getBooleanExtra("confirmation",false);
    if (loginControl == true) {
        ButtonClick2.setVisibility(View.GONE);
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }

    String price = getIntent().getStringExtra("Price");
    String model = getIntent().getStringExtra("Model");
    String imagePath = getIntent().getStringExtra("Image");
    ArrayList<String> properties = getIntent().getStringArrayListExtra(
            "Properties");

    txt1.setText(price);
    txt2.setText(model);
    txt3.setText(properties.toString());

    GC.Imageurl = GC.ImagePreurl + imagePath;

    new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute(GC.Imageurl);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    ArrayList<String> contactList = new ArrayList<String>();
    String name = "";
    String number = "";
    switch (reqCode) {
    case (0):
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            ContentResolver cr = getContentResolver();
            Cursor cur = getContentResolver().query(contactData, null,
                    null, null, null);
            if (cur.moveToFirst()) {
                name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null, null, null, null);
                    while (pCur.moveToNext()) {
                        number = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                    pCur.close();
                }
            }
            break;
        }
    }
    String contact = " isim            :" + name + "\n" + "  telefon no :"
            + number;
    contactList.add(contact);
    Intent intent = new Intent(IndexActivity.this, OrderActivity.class);
    intent.putStringArrayListExtra("Contacts", contactList);
    startActivity(intent);
}

例外..

08-14 08:01:51.908: E/AndroidRuntime(8847): java.lang.RuntimeException: Unable to start activity ComponentInfo{tr.com.turkcell.shmobile/tr.com.turkcell.shmobile.IndexActivity}: java.lang.NullPointerException
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.os.Looper.loop(Looper.java:137)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.ActivityThread.main(ActivityThread.java:4754)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at java.lang.reflect.Method.invoke(Method.java:511)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at dalvik.system.NativeStart.main(Native Method)
08-14 08:01:51.908: E/AndroidRuntime(8847): Caused by: java.lang.NullPointerException
08-14 08:01:51.908: E/AndroidRuntime(8847):     at tr.com.turkcell.shmobile.IndexActivity.onCreate(IndexActivity.java:78)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.Activity.performCreate(Activity.java:5008)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
08-14 08:01:51.908: E/AndroidRuntime(8847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-14 08:01:51.908: E/AndroidRuntime(8847):     ... 11 more

2 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并且在找到解决方案后没有深入研究,但尝试在参数中添加命名空间。实际值并不重要,但我的猜测是Android以不同方式处理“原始”键值以防止冲突。

final String PARCEL_CONFIRM="this.is.a.namespace.confirmation";

然后使用它与Intent进行交互。

答案 1 :(得分:0)