我在活动中有一个按钮n 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);
}*/
}
上面的代码是否正确?当给定输入时发生运行力关闭。但是当没有输入时,显示toast msg。
日志猫是
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);
}
}
答案 0 :(得分:0)
使用静态变量将参数传递给活动是一个错误。当您的应用程序在后台时,它可能会被系统杀死以恢复内存。如果应用程序随后移回到前面,您的活动将会恢复,但全局变量将不再存在。
请参阅此问题,将字符串从一个活动正确传递到另一个活动:Pass a String from one Activity to another Activity in Android
您应该执行以下操作:
Intent i = new Intent(this, Details.class);
i.putExtra("category", "Food");
startActivity(i);
并在Details.class中:
String category = intent.getExtras().getString("category");
因为您没有发布代码,所以引发异常的来源并不十分清楚。您确定“categ”变量是否已正确初始化为Details.category?无论如何,你应该使用意图附加。
答案 1 :(得分:0)
做一件事,你可以声明变量Globally并从oncreate而不是onclick初始化,
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);
因为我认为当您选择单选按钮时单选按钮为空时。 或者请在这里发布您的整个Activity java类以获得精确的解决方案。
答案 2 :(得分:0)
这些行告诉我们问题出在第109行的Dbpages.java中:
Caused by: java.lang.NullPointerException
at com.yellowpages.Dbpages.fetchRecord(Dbpages.java:109)
当变量为null
且您尝试访问其中一种方法时,会发生NullPointerException。
这是第109行:
Cursor c=mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_NAME,KEY_PHONE,KEY_CATEGORY,KEY_PINCODE},"KEY_PINCODE = ? AND KEY_CATEGORY =?",new String[]{pin,categ}, null, null, KEY_NAME);
您只是尝试访问一个变量mDb
并使用.query()
。但mDb
为null
,您从未对其进行初始化。因此出现NullPointerException。
<强>答案强>
在尝试查询之前,您需要使用mDb = ...
初始化数据库。