首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >空指针异常

空指针异常
EN

Stack Overflow用户
提问于 2013-04-06 03:28:36
回答 2查看 184关注 0票数 0

我快疯了,想弄清楚这个out.When,我点击我的活动,它会与NullPointerException.I崩溃。我尝试过调试,但我无法进入这个活动的问题。我尝试删除数据库,仍然崩溃。我尝试删除ListView,仍然崩溃。我尝试修改数据库,仍然崩溃。

由于一些奇怪的原因,当我删除按钮和它的onClickListener在“播放按钮”,我能够进入活动和一切在活动worked.Can有人帮助我为什么这个按钮使应用程序崩溃在活动之前,而不是在点击?

代码语言:javascript
复制
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)

活性

代码语言:javascript
复制
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();
                }
            });





    }

    }

playafriend xml

代码语言:javascript
复制
    <?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>

list_items xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:background="@drawable/searchimageview"
        android:orientation="vertical" >

        <Button
        android:id="@+id/playbutton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="right"
        android:background="@drawable/playbutton" />

<TextView 
    android:id="@+id/textview_friends"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:textStyle="bold"
    android:textSize="40dp"
    android:layout_gravity="center" />


    </FrameLayout>



</LinearLayout>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-06 04:36:00

请试试这个密码。

代码语言:javascript
复制
 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();
                        }
                    }); 
         }
    }

    }

如果你有什么问题,请告诉我。

票数 1
EN

Stack Overflow用户

发布于 2013-04-06 05:21:33

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15846726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档