このコードでボタンを削除するには、ブール値を返します。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