我的应用程序有两个活动和一个扩展ArrayAdapter的java文件。
MainActivity.java
package com.prajjwal.myfreight_0;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
final String[] comptype ={"Exporter","Shipper","Freight Forwarder","Custom House Agent","Transporter"};
String ss;
TextView tv1,tv2;
RadioGroup rg1;
RadioButton rb1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
rg1=(RadioGroup)findViewById(R.id.radioGroup1);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int selid=rg1.getCheckedRadioButtonId();
rb1=(RadioButton)findViewById(selid);
ss="did";
if(rb1.getText().toString().equals(comptype[0])) {
Intent i1=new Intent(MainActivity.this,FreeActivity.class);
i1.putExtra("did", comptype[0]);
startActivity(i1);
}
}
});
}
FreeActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ListView;
public class FreeActivity extends Activity {
ListView lw;
final String[] fields={"Name","User Name","E-mail","Company","Company E-mail"};
LoginListView llv1;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exp);
Intent i1;
i1=getIntent();
s=i1.getStringExtra("did");
llv1=new LoginListView(this,fields);
lw.setAdapter(llv1);
}
LoginListView助手类
package com.prajjwal.myfreight_0;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class LoginListView extends ArrayAdapter<String> {
private final Activity a1;
private final String[] s1;
public LoginListView(Activity a, String[] s){
super(a,R.layout.loginlist,s);
s1=s;
a1=a;
}
public View getView(int i,View v,ViewGroup vg) {
LayoutInflater li=a1.getLayoutInflater();
View v1=li.inflate(R.layout.loginlist, null, true);
TextView tv;
tv=(TextView)v1.findViewById(R.id.textView0);
tv.setText(s1[i]);
return v1;
}
}
loginlist.xml有一个TableLayout,其中一个TableRow包含一个TextView和一个EditText
activity_exp.xml有一个ListView和一个ImageView。我使用扩展ArrayAdapter的LoginListView类在listview中使用。每当我切换到FreeActivity.class时,我都会得到java.lang.NPE。我似乎无法解决错误。
答案 0 :(得分:0)
在使用ListView之前实例化ListView。我很确定您的LogCat会告诉您一些相关信息。
public class FreeActivity extends Activity {
ListView lw;
final String[] fields={"Name","User Name","E-mail","Company","Company E-mail"};
LoginListView llv1;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exp);
Intent i1;
i1=getIntent();
s=i1.getStringExtra("did");
llv1=new LoginListView(this,fields);
lw = new ListView(this);//add this
lw.setAdapter(llv1);
}
此外,练习使用适当的变量名称以提高可读性。使用毫无意义的字母会让你和我们在努力帮助你时变得困难。