私は活動中の1つのボタンと6つのラジオボタンを持っています。ボタンをクリックして編集テキストから入力を取得した後(検索)、別のアクティビティに移動します。以下は私のコードです
public void search(View v)
{
switch(v.getId())
{
case R.id.button1:
EditText et1= (EditText)findViewById(R.id.editText1);
RadioButton r1 = (RadioButton) findViewById(R.id.radio0);
RadioButton r2 = (RadioButton) findViewById(R.id.radio1);
RadioButton r3 = (RadioButton) findViewById(R.id.radio2);
RadioButton r4 = (RadioButton) findViewById(R.id.radio3);
RadioButton r5 = (RadioButton) findViewById(R.id.radio4);
RadioButton r6 = (RadioButton) findViewById(R.id.radio5);
pin=et1.getText().toString();
if(pin.length()==0){
Toast.makeText(this, "Please provide Input!", Toast.LENGTH_SHORT).show();
return;
}
if(r1.isChecked())
{ category="All";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r2.isChecked())
{
category="Food";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r3.isChecked())
{
category="Entertainment";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r4.isChecked())
{
category="Shopping";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r5.isChecked())
{
category="Health";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if (r6.isChecked())
{
category="Services";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
break;
}
/*if(pin.length()!=0&&category.length()!=0)
{
Intent i=new Intent(this,Details.class);
startActivity(i);
}*/
}
上記のコードは正しいですか?? 入力が与えられたときに実行中の強制終了が発生しますが、入力がない場合はトーストメッセージが表示されます。
丸太猫は
09-29 23:56:25.706: E/AndroidRuntime(23196): FATAL EXCEPTION: main
09-29 23:56:25.706: E/AndroidRuntime(23196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yellowpages/com.yellowpages.Details}: java.lang.NullPointerException
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.os.Looper.loop(Looper.java:137)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-29 23:56:25.706: E/AndroidRuntime(23196): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 23:56:25.706: E/AndroidRuntime(23196): at java.lang.reflect.Method.invoke(Method.java:511)
09-29 23:56:25.706: E/AndroidRuntime(23196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-29 23:56:25.706: E/AndroidRuntime(23196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-29 23:56:25.706: E/AndroidRuntime(23196): at dalvik.system.NativeStart.main(Native Method)
09-29 23:56:25.706: E/AndroidRuntime(23196): Caused by: java.lang.NullPointerException
09-29 23:56:25.706: E/AndroidRuntime(23196): at com.yellowpages.Dbpages.fetchRecord(Dbpages.java:109)
09-29 23:56:25.706: E/AndroidRuntime(23196): at com.yellowpages.Details.onCreate(Details.java:22)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.Activity.performCreate(Activity.java:4465)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-29 23:56:25.706: E/AndroidRuntime(23196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-29 23:56:25.706: E/AndroidRuntime(23196): ... 11 more
package com.yellowpages;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class UserSearch extends Activity {
public static String category="",pin="";
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_search);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_user_search, menu);
return true;
}
public void search(View v)
{
switch(v.getId())
{
case R.id.button1:
EditText et1= (EditText)findViewById(R.id.editText1);
RadioButton r1 = (RadioButton) findViewById(R.id.radio0);
RadioButton r2 = (RadioButton) findViewById(R.id.radio1);
RadioButton r3 = (RadioButton) findViewById(R.id.radio2);
RadioButton r4 = (RadioButton) findViewById(R.id.radio3);
RadioButton r5 = (RadioButton) findViewById(R.id.radio4);
RadioButton r6 = (RadioButton) findViewById(R.id.radio5);
pin=et1.getText().toString();
if(pin.length()==0){
Toast.makeText(this, "Please provide Input!", Toast.LENGTH_SHORT).show();
return;
}
if(r1.isChecked())
{ category="All";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r2.isChecked())
{
category="Food";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r3.isChecked())
{
category="Entertainment";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r4.isChecked())
{
category="Shopping";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if(r5.isChecked())
{
category="Health";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
else if (r6.isChecked())
{
category="Services";
Intent i=new Intent(this,Details.class);
startActivity(i);
}
break;
}
/*if(pin.length()!=0&&category.length()!=0)
{
Intent i=new Intent(this,Details.class);
startActivity(i);
}*/
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}