当我调用此Activity时,会出现Null Pointer Exception。为什么会这样?
package com.andrd.gps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class User_Detail_ListActivity extends Activity{
TransportData tdata;
private TextView drivernametxt,agetxt,addresstxt,liecencetxt,contactNotxt,driverTypetxt,truckNotxt,truckPermittxt,truckTypetxt,fromtxt,totxt;
Button editBtn,deleteBtn,cancelBtn;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
drivernametxt = (TextView) findViewById(R.id.nameTextView);
agetxt=(TextView) findViewById(R.id.ageTextView);
addresstxt=(TextView) findViewById(R.id.addressTextView);
liecencetxt=(TextView) findViewById(R.id.licenseNoTextView);
contactNotxt=(TextView) findViewById(R.id.contactNoTextView);
driverTypetxt=(TextView) findViewById(R.id.employmetnTypeTextView);
truckNotxt=(TextView) findViewById(R.id.truckNoTextView);
truckPermittxt=(TextView) findViewById(R.id.truckPermitTextView);
truckPermittxt=(TextView) findViewById(R.id.truckTypeTextView);
fromtxt=(TextView) findViewById(R.id.fromTextView);
totxt=(TextView) findViewById(R.id.toTextView);
editBtn =(Button) findViewById(R.id.editBtn);
deleteBtn = (Button) findViewById(R.id.deleteBtn);
cancelBtn = (Button) findViewById(R.id.cancelBtn);
drivernametxt.setText(getIntent().getExtras().getString("DriverName"));
agetxt.setText(getIntent().getExtras().getString("Age"));
addresstxt.setText(getIntent().getExtras().getString("Address"));
liecencetxt.setText(getIntent().getExtras().getString("LiecenseNo"));
contactNotxt.setText(getIntent().getExtras().getString("ContactNo"));
driverTypetxt.setText(getIntent().getExtras().getString("DriverType"));
truckNotxt.setText(getIntent().getExtras().getString("TruckNo"));
truckPermittxt.setText(getIntent().getExtras().getString("TruckPermit"));
truckTypetxt.setText(getIntent().getExtras().getString("TruckType"));
fromtxt.setText(getIntent().getExtras().getString("FromLocation"));
totxt.setText(getIntent().getExtras().getString("ToLocation"));
editBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(User_Detail_ListActivity.this, EditUserDetailActivity.class);
intent.putExtra("TruckNo", getIntent().getExtras().getString("TruckNo"));
startActivity(intent);
}
});
deleteBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
try {
tdata.deleteUser(getIntent().getExtras().getString("TruckNo"));
finish();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "database Error", Toast.LENGTH_LONG).show();
}
}
});
cancelBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
finish();
}
});
}
}
答案 0 :(得分:1)
确保将活动添加到Application Manifest
<activity android:name=".EditUserDetailActivity"></activity>
答案 1 :(得分:1)
通常在Java中,NullPointerException的原因是:
但在这种情况下,您可能无法在上一个Activity中设置Extras,或者您的getString()方法参数值可能不正确
答案 2 :(得分:0)
嗨Mukesh我在初始化TextView时犯了一个错误。
请检查您是否已初始化truckPermittxt
两次。初始化truckTypetxt
那里&amp;然后尝试使用truckTypetxt.setText(getIntent().getExtras().getString("TruckType"));
这是NullPointerException的主要原因。
答案 3 :(得分:0)
你从未初始化truckTypetxt
...而是将这个初始化两次。
truckPermittxt=(TextView) findViewById(R.id.truckPermitTextView);
truckPermittxt=(TextView) findViewById(R.id.truckTypeTextView);
但你使用truckTypetxt
truckTypetxt.setText(getIntent().getExtras().getString("TruckType"));