Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android emulator中的java.lang.NullPointerException_Android_Android Emulator_Runtimeexception - Fatal编程技术网

android emulator中的java.lang.NullPointerException

android emulator中的java.lang.NullPointerException,android,android-emulator,runtimeexception,Android,Android Emulator,Runtimeexception,我正在制作一个应用程序,其中我创建了一个列表视图,其中包含两个文本字段和一个复选框……当我单击或选中复选框时,这两个文本都被添加到数据库中。但是我的模拟器力量停止了我的应用程序。请提前帮助…thanx 这是我的第一个名为Database2Activity的主java类 package data.base; import android.app.ListActivity; import android.os.Bundle; public class Database2Activity extend

我正在制作一个应用程序,其中我创建了一个列表视图,其中包含两个文本字段和一个复选框……当我单击或选中复选框时,这两个文本都被添加到数据库中。但是我的模拟器力量停止了我的应用程序。请提前帮助…thanx

这是我的第一个名为Database2Activity的主java类

package data.base;
import android.app.ListActivity;
import android.os.Bundle;
public class Database2Activity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] str1 = new String[]{"Hari","Gopal","shayam","sita"};
    String[] str2 = new String[]{"123","2555","sksi","mahi"};
    setListAdapter(new Second(this, str1, str2));   
}
}
这是我的第二个类,名为Second.java

package data.base;

import com.example.phone_no.DBhelper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class Second extends ArrayAdapter<String> {
 String string1[];

 Context context;
    String string[];
     TextView textView;
    TextView textView1;

    CheckBox checkBox;
    EditText editText;
    public Second(Context context, String[] objects, String[] Object1)
    {

        super(context,R.layout.main, objects);

        this.context=context;
        this.string=objects;
        this.string1=Object1;
      //  this.imageView = Object3;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.main,parent,false);
          textView=(TextView)view.findViewById(R.id.text1);


        textView1=(TextView)view.findViewById(R.id.text2);
       // imageView = (ImageView)view.findViewById(R.id.text3);
        textView.setText(string[position]);
        textView1.setText(string1[position]);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub

                DBhelper DB = new DBhelper(context);
                DB.open();
                DB.adddata("A", string[position], string1[position]);
                DB.getAlldata();
                DB.close();


            }
        });

        return view;    //To change body of overridden methods use File | Settings | File Templates.

    }

}
这是我的main.xml

<?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:orientation="horizontal" >

<EditText
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/edit"
/>
<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
     />
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:layout_weight="1"
        android:id="@+id/text1"
        />
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:layout_weight="1"
        android:id="@+id/text2"
        />
    <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/check"
            />



</LinearLayout>
最后这是我的舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="data.base"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Database2Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="Second"/>
</application>

</manifest>

您需要在ListActivity的布局(
main.xml
)中使用id为
android:id=“@android:id/list
的ListView

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  // This layout must have a ListView
您需要在ListActivity的布局(
main.xml
)中使用id为
android:id=“@android:id/list
的ListView

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  // This layout must have a ListView


移除
setContentView(R.layout.main)onCreate
-您正在使用每个元素所使用的布局覆盖
ListActivity
(其中包含android.R.id.list元素)的默认视图。

删除
setContentView(R.layout.main)从您的
onCreate
-您正在使用每个元素所使用的布局覆盖
ListActivity
(包含android.R.id.list元素)的默认视图。

如何更改ListView的id。我是这个领域的新手,所以请解释一下你的解决方案好吧,你的活动和行布局都使用相同的布局。活动的布局中应该有一个ListView,行布局不应该…看起来您想使用Listactivity的默认布局,所以删除
setContentView(R.layout.main)。或者使用ListView创建一个新的XML文件,并在
setContentView(R.layout.ListView\u layout)中使用它我删除“setContentView”(R.layout.main)。。。。。。。。。但是我的代码仍然给我空指针执行选项…我如何更改ListView的id。我是这个领域的新手,所以请解释一下你的解决方案好吧,你的活动和行布局都使用相同的布局。活动的布局中应该有一个ListView,行布局不应该…看起来您想使用Listactivity的默认布局,所以删除
setContentView(R.layout.main)。或者使用ListView创建一个新的XML文件,并在
setContentView(R.layout.ListView\u layout)中使用它我删除“setContentView”(R.layout.main)。。。。。。。。。但是我的代码仍然给我空指针执行选项……同意,作者在活动和适配器中都使用
main.xml
。看起来它应该只在适配器中使用。是的,当我再次运行代码时,它会显示错误=“03-12 02:04:21.308:E/AndroidRuntime(21905):java.lang.NullPointerException 03-12 02:04:21.308:E/AndroidRuntime(21905):at data.base.Second.getView(Second.java:51)”@dev这是一个进步在适配器中更改
super(context,R.layout.main,objects)
super(上下文,R.layout.main,R.id.text1,对象)@ianhanniballake不,它不工作。。。。。。我不理解你的解决方案,超级是默认的here@dev-第51行出现错误,因为您从未初始化复选框-确保您有一个复选框checkBox=(checkBox)view.findById(R.id.check);同意,作者在活动和适配器中都使用
main.xml
。看起来它应该只在适配器中使用。是的,当我再次运行代码时,它会显示错误=“03-12 02:04:21.308:E/AndroidRuntime(21905):java.lang.NullPointerException 03-12 02:04:21.308:E/AndroidRuntime(21905):at data.base.Second.getView(Second.java:51)”@dev这是一个进步在适配器中更改
super(context,R.layout.main,objects)
super(上下文,R.layout.main,R.id.text1,对象)@ianhanniballake不,它不工作。。。。。。我不理解你的解决方案,超级是默认的here@dev-第51行出现错误,因为您从未初始化复选框-确保您有一个复选框checkBox=(checkBox)view.findById(R.id.check);你应该观看谷歌I/O对话,他们将帮助你编写更快的适配器。你应该观看谷歌I/O对话,他们将帮助你编写更快的适配器。
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  // This layout must have a ListView