我有一个代码来检查PNR号码。由于互联网问题,我想让用户通过短信检查PNR。因此,我添加了2个单选按钮,一个用于Internet,一个用于SMS。但是现在的问题是,当我单击PNR按钮时,它给出了nullPointer异常。
这是我的主要Activity.java
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private EditText pnrNumber;
private TextView errMsg;
private Button getPnr;
private Button pnrClear;
Button Yes, No;
RadioButton checkbyinternet, checkbysms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
errMsg = (TextView) findViewById(R.id.errMsg);
pnrNumber = (EditText) findViewById(R.id.pnrNumber_p01);
getPnr = (Button) findViewById(R.id.checkPNRButton);
pnrClear = (Button) findViewById(R.id.pnrClear);
getPnr.setOnClickListener(this);
pnrClear.setOnClickListener(this);
}
public void onClick(View src) {
// Perform action on click
if (src.getId() == R.id.checkPNRButton)
{
if (checkbyinternet.isChecked())
{
int pnr2 = pnrNumber.getEditableText().length();
if (pnr2 != 10)
{
errMsg.setText("Length of PNR is Invalid.");
}
else
{
String pnr = pnrNumber.getEditableText().toString();
Bundle b = new Bundle();
b.putString("pnr", pnr);
System.out.println("Connectivity : "
+ this.isNetworkAvailable());
PNRStatus pnrStatus = null;
// Connect to the Server and Get the PNR status
try
{
String captcha = "37819";
String pnr1 = pnrNumber.getText().toString();
String reqStr = "lccp_pnrno1=" + pnr1
+ "&lccp_cap_val=" + captcha
+ "&lccp_capinp_val=" + captcha
+ "&submitpnr=Get+Status";
PNRStatusCheck check = new PNRStatusCheck();
StringBuffer data = check
.getPNRResponse(reqStr,
"http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_28688.cgi");
// String pnr1 = pnr; //"1154177041";
// String reqStr = "lccp_pnrno1=" + pnr1 +
// "&submitpnr=Get+Status";
// PNRStatusCheck check = new PNRStatusCheck();
// StringBuffer data = check.getPNRResponse(reqStr,
// "http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi");
if (data != null)
{
pnrStatus = check.parseHtml(data);
b.putSerializable("pnrStatus", pnrStatus);
}
else
{
// error
}
}
catch (Exception e)
{
e.printStackTrace();
}
Intent to = null;
if (pnrStatus != null)
{
to = new Intent(this, PNRStatusActivity.class);
to.putExtras(b);
startActivity(to);
}
else
{
errMsg.setText("Error prcessing PNR. Please try again.");
}
}
}
else if (checkbysms.isChecked())
{
// Toast.makeText(this, "SMS", Toast.LENGTH_SHORT).show();
int pnr2 = pnrNumber.getEditableText().length();
if (pnr2 != 10)
{
errMsg.setText("Length of PNR is Invalid.");
}
else
{
openSMSWarningDialog(src);
}
}
}
else if (src.getId() == R.id.pnrClear)
{
errMsg.setText("");
pnrNumber.setText("");
}
}
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null, otherwise check
// if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
public void openSMSWarningDialog(View view) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.smsdialog);
dialog.setTitle("Are you sure to use SMS.?");
Yes = (Button) dialog.findViewById(R.id.yes);
No = (Button) dialog.findViewById(R.id.no);
Yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String PnrNum = pnrNumber.getText().toString();
String messageToSend = ("PNR " + PnrNum);
String number = "139";
SmsManager.getDefault().sendTextMessage(number, null,
messageToSend, null, null);
dialog.dismiss();
Toast.makeText(
getBaseContext(),
"Please check your inbox in sometime for your PNR Status",
Toast.LENGTH_LONG).show();
}
});
No.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:text="@string/title"
android:gravity="center_horizontal"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/errMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:textColor="@android:color/black"
android:text="10 Digits Mandatory" />
<EditText
android:id="@+id/pnrNumber_p01"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:paddingLeft="10dp"
android:background="@drawable/edittextellipsedbackground"
android:layout_marginTop="17dp"
android:layout_marginBottom="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:hint="@string/pnrTextView" >
<requestFocus />
</EditText>
<Button
android:id="@+id/checkPNRButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/bluebutton"
android:layout_marginTop="17dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:padding="10dp"
android:shadowColor="#000000"
android:shadowRadius="5.9"
android:text="@string/checkPNRButton"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/pnrClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bluebutton"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:padding="10dp"
android:shadowColor="#000000"
android:shadowRadius="5.9"
android:text="Clear"
android:textColor="#ffffff"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="20dp"
android:background="@drawable/roundlayoutborder"
android:gravity="center"
android:paddingBottom="5dp" >
<RadioGroup
android:id="@+id/checkvia"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="vertical" >
<RadioButton
android:id="@+id/internet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textColor="@android:color/black"
android:text="@string/CheckByInternet"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="@string/CheckBySMS"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
短信布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9bafb0"
android:orientation="vertical" >
<TextView
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="#ff0000"
android:ems="27"
android:text="@string/CheckThroughSMSWarning" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp" >
<Button
android:id="@+id/yes"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="2dp"
android:textColor="@android:color/white"
android:layout_weight="1"
android:background="@drawable/bluebutton"
android:text="Yes" />
<Button
android:id="@+id/no"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:textColor="@android:color/white"
android:layout_weight="1"
android:background="@drawable/bluebutton"
android:text="No" />
</LinearLayout>
</LinearLayout>
这是我的日志:
02-06 13:54:22.810: E/AndroidRuntime(858): FATAL EXCEPTION: main
02-06 13:54:22.810: E/AndroidRuntime(858): java.lang.NullPointerException
02-06 13:54:22.810: E/AndroidRuntime(858): at akshat.jaiswal.indianrailways.MainActivity.onClick(MainActivity.java:50)
02-06 13:54:22.810: E/AndroidRuntime(858): at android.view.View.performClick(View.java:4084)
02-06 13:54:22.810: E/AndroidRuntime(858): at android.view.View$PerformClick.run(View.java:16966)
02-06 13:54:22.810: E/AndroidRuntime(858): at android.os.Handler.handleCallback(Handler.java:615)
02-06 13:54:22.810: E/AndroidRuntime(858): at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 13:54:22.810: E/AndroidRuntime(858): at android.os.Looper.loop(Looper.java:137)
02-06 13:54:22.810: E/AndroidRuntime(858): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-06 13:54:22.810: E/AndroidRuntime(858): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 13:54:22.810: E/AndroidRuntime(858): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 13:54:22.810: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-06 13:54:22.810: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-06 13:54:22.810: E/AndroidRuntime(858): at dalvik.system.NativeStart.main(Native Method)
请帮忙,我无法为此找到任何解决方案。
最佳答案
您的checkbyinternet
和checkbysms
按钮未初始化,这就是在NullPointerException
方法中执行if (checkbyinternet.isChecked())
时得到onClick()
的原因。
if (src.getId() == R.id.checkPNRButton) // true if you pressed the getPnr button
{
if (checkbyinternet.isChecked()) // checkbyinternet is uninitialized yet, so it'll throw a NPE
您还需要在
onCreate()
方法中对其进行初始化。checkbyinternet = (RadioButton) findViewById(R.id.internet);
checkbysms = (RadioButton) findViewById(R.id.sms);