Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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
Java 空指针异常_Java_Android_Android Listview_Nullpointerexception - Fatal编程技术网

Java 空指针异常

Java 空指针异常,java,android,android-listview,nullpointerexception,Java,Android,Android Listview,Nullpointerexception,我正在疯狂地尝试找出这个问题。当我单击我的活动时,它会崩溃,出现NullPointerException。我尝试过调试,但无法进入相关活动。我尝试过删除数据库,但仍然崩溃。我尝试过删除ListView,仍然崩溃。我尝试过修改数据库,仍然崩溃 出于某种奇怪的原因,当我在“playbutton”上移除按钮及其onClickListener时,我能够进入活动,活动中的一切都正常工作。有人能帮我解释一下为什么这个按钮会使应用程序在活动之前崩溃,而不是在onclick上崩溃吗 04-05 23:15:15

我正在疯狂地尝试找出这个问题。当我单击我的活动时,它会崩溃,出现NullPointerException。我尝试过调试,但无法进入相关活动。我尝试过删除数据库,但仍然崩溃。我尝试过删除ListView,仍然崩溃。我尝试过修改数据库,仍然崩溃

出于某种奇怪的原因,当我在“playbutton”上移除按钮及其onClickListener时,我能够进入活动,活动中的一切都正常工作。有人能帮我解释一下为什么这个按钮会使应用程序在活动之前崩溃,而不是在onclick上崩溃吗

04-05 23:15:15.886: E/AndroidRuntime(11445): FATAL EXCEPTION: main
04-05 23:15:15.886: E/AndroidRuntime(11445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fullfrontalgames.numberfighter/com.fullfrontalgames.numberfighter.PlayAFriend}: java.lang.NullPointerException
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.os.Looper.loop(Looper.java:137)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.ActivityThread.main(ActivityThread.java:5059)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at java.lang.reflect.Method.invoke(Method.java:511)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at dalvik.system.NativeStart.main(Native Method)
04-05 23:15:15.886: E/AndroidRuntime(11445): Caused by: java.lang.NullPointerException
04-05 23:15:15.886: E/AndroidRuntime(11445):    at com.fullfrontalgames.numberfighter.PlayAFriend.onCreate(PlayAFriend.java:54)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.Activity.performCreate(Activity.java:5058)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-05 23:15:15.886: E/AndroidRuntime(11445):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
活动

package com.fullfrontalgames.numberfighter;
import com.fullfrontalgames.numberfighter.DBAdapter;
import com.fullfrontalgames.numberfighter.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;



import android.os.Bundle;

import android.support.v4.widget.SimpleCursorAdapter;


import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;




public class PlayAFriend extends Activity{
    DBAdapter DBAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playafriend);

        final DBAdapter db = new DBAdapter(this);
        DBAdapter = db.open();

        ListView FriendLV = (ListView) findViewById(android.R.id.list);
        Button playbutton = (Button) findViewById(R.id.playbutton);
        final TextView friend = (TextView) findViewById(R.id.textview_friends);



        Cursor friendslist = db.GetAllFriends();

        String[] from = new String[] {"FRIENDS"};   // your column/columns here
        int[] to = new int[] {R.id.textview_friends};

        @SuppressWarnings("deprecation")
        ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_items, friendslist, from, to,0);
          FriendLV.setAdapter(cursorAdapter);



          playbutton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String Defender = friend.getText().toString();
                    db.GetFriend(Defender);
                    startActivity(new Intent ("com.fullfrontalgames.numberfighter.Fightattacker"));
                    db.close();
                }
            });





    }

    }
PlayafriendXML

    <?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:background="@drawable/nfbackground"
    android:orientation="vertical" >

    <ImageView
        android:id="@id/titlebar"
        android:layout_width="wrap_content"
        android:layout_height="66dp"
        android:src="@drawable/nftitlebar" />


        <ListView 
            android:id="@android:id/list"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >







        </ListView>






</LinearLayout>

列表项xml


请尝试此代码

 public class Test extends Activity{
        DBAdapter DBAdapter;
        ListView FriendLV;
        Button playbutton;
        final TextView friend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playafriend);
        final DBAdapter db = new DBAdapter(this);
        db.open();
        Cursor friendslist = db.GetAllFriends();
         if(friendslist.getCount()!=0){
             FriendLV = (ListView) findViewById(android.R.id.list);
             playbutton = (Button) findViewById(R.id.playbutton);
             friend = (TextView) findViewById(R.id.textview_friends);
             String[] from = new String[] {"FRIENDS"};   // your column/columns here
                int[] to = new int[] {R.id.textview_friends};

                @SuppressWarnings("deprecation")
                ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_items, friendslist, from, to,0);
                  FriendLV.setAdapter(cursorAdapter);
                  playbutton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            String Defender = friend.getText().toString();
                            db.GetFriend(Defender);
                            startActivity(new Intent ("com.fullfrontalgames.numberfighter.Fightattacker"));
                            db.close();
                        }
                    }); 
         }
    }

    }

如果您遇到任何问题,请告诉我。

如果使用listview,您的活动应该扩展ListActivity

请添加完整的stacktrace。您可能不应该让这些行DBAdapter DBAdapter;DBAdapter=db.open();而是db.open();应该足够了。无论如何,friend或db都是null。请用错误消息发布logcat。至少我想说我们已经回答了您的空指针问题。如果您的
ListView
在布局文件中,那么您应该将其id定义为
@android:id/list
,您应该在java文件中通过将其初始化为
ListView FriendLV=(ListView)findViewById来访问它(R.id.list);
。这样试试吧。我的建议如下:让你的活动成为一个列表活动;按照Grishu说的做:ListView FriendLV=(ListView)findViewById(R.id.list);在playafriend布局中添加一个。在线性布局下将playafriend布局中添加PlayaButton(从列表项布局中删除);向列表中添加一个McClickListener,并在其中添加代码以管理单击项目列表(因此您可能不需要播放按钮)。如果你想让播放按钮出现在每个列表项中,那么我建议你在谷歌上搜索可点击的列表项。我的空点已解决,但我尝试了此代码按钮不起作用。我只需要对此采取不同的方法,或者改为制作一个新的监听器,然后放弃播放按钮。不,这没有什么区别。播放按钮显示出来了除了我在列表下面的那一个之外,我没有再点击他们。实际上你必须面对什么问题?这个问题的标题是关于一个空点异常,我不知道,但是从问题的结果来看,空点已经解决了,现在我没有一个按钮可以在这个列表视图设置上使用,因为这导致了nullpoint.在您的listview中,如果每一行都包含按钮,那么您必须在android中创建自定义适配器。如下所示。
 public class Test extends Activity{
        DBAdapter DBAdapter;
        ListView FriendLV;
        Button playbutton;
        final TextView friend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playafriend);
        final DBAdapter db = new DBAdapter(this);
        db.open();
        Cursor friendslist = db.GetAllFriends();
         if(friendslist.getCount()!=0){
             FriendLV = (ListView) findViewById(android.R.id.list);
             playbutton = (Button) findViewById(R.id.playbutton);
             friend = (TextView) findViewById(R.id.textview_friends);
             String[] from = new String[] {"FRIENDS"};   // your column/columns here
                int[] to = new int[] {R.id.textview_friends};

                @SuppressWarnings("deprecation")
                ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_items, friendslist, from, to,0);
                  FriendLV.setAdapter(cursorAdapter);
                  playbutton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            String Defender = friend.getText().toString();
                            db.GetFriend(Defender);
                            startActivity(new Intent ("com.fullfrontalgames.numberfighter.Fightattacker"));
                            db.close();
                        }
                    }); 
         }
    }

    }