我只是想问一下,当我想运行这个MainActivity
时出现错误,它说java.lang.NullPointerexecption
,我不知道为什么会发生这种情况,继承我的代码,
CustomAdapter.java
package com.example.search;
import com.example.database.search.Jurusan;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Jurusan>
{
Jurusan[] modelItems = null;
Context context;
public CustomAdapter(Context context, Jurusan[] resource) {
super(context,R.layout.row,resource);
// TODO Auto-generated constructor stub
this.context = context;
this.modelItems = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, parent, false);
//setting layout
TextView name = (TextView) convertView.findViewById(R.id.textView1);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
name.setText(modelItems[position].getName());
if(modelItems[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
我有MainActivity.java
package com.example.search;
import java.util.ArrayList;
import com.example.database.search.Jurusan;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class CreateData_J extends Activity
{
ListView lv;
Jurusan[] modelItems;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.create_data_j);
lv = (ListView) findViewById(R.id.listView1);
modelItems = new Jurusan[5];
modelItems[0] = new Jurusan("pizza", 0);
modelItems[1] = new Jurusan("burger", 1);
modelItems[2] = new Jurusan("olives", 1);
modelItems[3] = new Jurusan("orange", 0);
modelItems[4] = new Jurusan("tomato", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
}
这是我的Jurusan.java
public class Jurusan
{
String name;
int value; /* 0 -> checkbox disable, 1 -> checkbox enable */
Model(String name, int value)
{
this.name = name;
this.value = value;
}
public String getName()
{
return this.name;
}
public int getValue()
{
return this.value;
}
}
这是我的错误追踪
04-09 11:58:33.765: E/AndroidRuntime(22047): FATAL EXCEPTION: main
04-09 11:58:33.765: E/AndroidRuntime(22047): java.lang.NullPointerException
04-09 11:58:33.765: E/AndroidRuntime(22047): at com.example.search.CustomAdapter.getView(CustomAdapter.java:35)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.ListView.onMeasure(ListView.java:1158)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.View.measure(View.java:15592)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.View.measure(View.java:15592)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.View.measure(View.java:15592)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.View.measure(View.java:15592)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-09 11:58:33.765: E/AndroidRuntime(22047): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2180)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.View.measure(View.java:15592)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1878)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4356)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.os.Handler.handleCallback(Handler.java:725)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.os.Looper.loop(Looper.java:137)
04-09 11:58:33.765: E/AndroidRuntime(22047): at android.app.ActivityThread.main(ActivityThread.java:5099)
04-09 11:58:33.765: E/AndroidRuntime(22047): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 11:58:33.765: E/AndroidRuntime(22047): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 11:58:33.765: E/AndroidRuntime(22047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
04-09 11:58:33.765: E/AndroidRuntime(22047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
04-09 11:58:33.765: E/AndroidRuntime(22047): at dalvik.system.NativeStart.main(Native Method)
任何人都可以帮我解决这个错误,需要任何帮助谢谢你,对不起我的坏事 英语。谢谢
这是我的row.xml
<?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:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
这是我的activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
答案 0 :(得分:0)
根据您的错误日志,您似乎在
行的适配器类中有空指针异常name.setText(modelItems[position].getName());
这意味着&#34; name&#34;没有从布局得到正确意味着你的布局没有在适配器的控制下进行修改。 所以我正在修改你的适配器代码 试着希望它能解决这个问题。
package com.example.search;
import com.example.database.search.Jurusan;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Jurusan>
{
Jurusan[] modelItems = null;
Context context;
public CustomAdapter(Context context, Jurusan[] resource) {
super();
// TODO Auto-generated constructor stub
this.context = context;
this.modelItems = resource;
}
@Override
public View getView(int position, View convertview, ViewGroup parent)
{
View convertView = convertview;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.row, null);
}
TextView name = (TextView) convertView.findViewById(R.id.textView1);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
name.setText(modelItems[position].getName());
if(modelItems[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
如果它有用,那么投票
如果您有任何问题,我会在这里回答。
答案 1 :(得分:0)
尝试更改
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, parent, false);
到
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
即使以上工作原理,您也应该尝试在膨胀之前检查convertView是否为null。因为如果你这样做就会耗尽你的资源。