-2
我,馬新的Android編程,我經歷了在使用上有問題listView.setAdapter();
列表視圖集適配器沒有在Android Studio中工作
存在不能正常工作,並給予java.lang.NullPointerException
有代碼我的項目名稱是notbad
EditText textheare;
CheckBox checkBox;
ImageView imageView;
public static final int PICK_IMAGE=1;
Uri selectimage;
Button finn;
ListView listView;
coustem_laout Cous;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nodbatplus);
subject=(EditText)findViewById(R.id.subject);
textheare =(EditText)findViewById(R.id.textheare);
checkBox =(CheckBox)findViewById(R.id.checkBox);
imageView =(ImageView)findViewById(R.id.notimg);
finn=(Button)findViewById(R.id.Fin);
listView=(ListView)findViewById(R.id.nameList);
finn.setOnClickListener(this);
checkBox.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(checkBox.isChecked()){
opengallary();
}
else if (checkBox.isChecked()==false)
imageView.setImageBitmap(null);
}
public void opengallary(){
Intent gallary=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallary,PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==PICK_IMAGE && resultCode==RESULT_OK)
selectimage=data.getData();
imageView.setImageURI(selectimage);
}
@Override
public void onClick(View v) {
String n=textheare.getText().toString();
String s=subject.getText().toString();
int i = imageView.getImageAlpha();
SharedPreferences ss =getSharedPreferences(s,MODE_PRIVATE);
SharedPreferences.Editor editor=ss.edit();
editor.putString("notbadtext",n);
editor.putInt("imagehere",i);
editor.commit();
Cous=new coustem_laout(this,android.R.layout.simple_list_item_1,imageView,subject);
listView.setAdapter(Cous);
finish();
}
且有xml文件按照notbatplus:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/textheare"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/subject" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_marginTop="28dp"
android:id="@+id/subject"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="26dp"
android:layout_marginTop="29dp"
android:id="@+id/notimg" />
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_alignBottom="@+id/imageView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="65dp"
android:layout_marginBottom="28dp" />
<Button
android:text="Finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/checkBox"
android:layout_marginBottom="37dp"
android:id="@+id/Fin" />
</RelativeLayout>
</LinearLayout>
並有mainActivite:
Button bn;
ListView listView;
notbad no;
String subnot;
int immg;
SharedPreferences sharedPreferences;
coustem_laout Coustem_laout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn=(Button)findViewById(R.id.addlist);
listView=(ListView)findViewById(R.id.nameList);
bn.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
listView.setVisibility(View.VISIBLE);
Intent i =new Intent(MainActivity.this,notbad.class);
startActivity(i);
}
有XML文件,遵循mainActivite:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.qan.broedc.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:id="@+id/nameList"
android:fastScrollAlwaysVisible="true"
android:addStatesFromChildren="false"
android:visibility="gone" />
<Button
android:text="add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="77dp"
android:layout_marginTop="42dp"
android:id="@+id/addlist"
android:onClick="moveto" />
</RelativeLayout>
類適配器:
public class coustem_laout extends ArrayAdapter {
public final Context c;
public int laout;
public ImageView IMG;
public EditText Texthear;
public View view;
ImageView Addressimage;
TextView sub;
LayoutInflater layoutInflater;
public coustem_laout(Context context, int resource,ImageView img,EditText editText) {
super(context, resource);
this.c = context;
laout=resource;
IMG=img;
Texthear=editText;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
layoutInflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(laout,parent,false);
sub=(TextView)view.findViewById(R.id.name_list);
Addressimage=(ImageView)view.findViewById(R.id.imagelist);
sub.setText(Texthear.getText().toString());
Addressimage.setImageAlpha(IMG.getImageAlpha());
return view;
}
}
有錯誤:
11-30 04:10:42.909 1427-1427/com.example.qan.broedc E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.qan.broedc.notbad.onClick(notbad.java:101)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
你的適配器類在哪裏? –
你能告訴我們NullPointerException的堆棧跟蹤嗎?同時告訴我們發生了什麼事情。 –
notbad xml佈局中沒有列表視圖,因此列表視圖爲空。很明顯 – Manza