首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

一添加监听器就错误,帮小弟我看看吧,多谢

2012-06-16 
一添加监听器就异常,帮我看看吧,谢谢程序是一个小播放器,当然还只完成不一部分。先看MainActivityJava code

一添加监听器就异常,帮我看看吧,谢谢
程序是一个小播放器,当然还只完成不一部分。
先看MainActivity

Java code
package wangxin.Activity;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import wangxin.file.FileUtils;import wangxin.mode.Mp3;import android.app.Activity;import android.app.AlertDialog;import android.app.ListActivity;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {    public static final int NEWSONGMENU = 0;    public static final int TOOLMENU = 5;    public static final int ABOUTMENU = 10;    private List<Mp3> mp3Files = null;    public ListView listView = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                        listView = (ListView) findViewById(R.id.listView);        listView.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,                    long arg3) {                // TODO Auto-generated method stub                Mp3 mp3 = mp3Files.get(arg2);                Intent intent = new Intent();                intent.putExtra("mp3Song", mp3);                intent.setClass(MainActivity.this, PlayerActivity.class);                startActivity(intent);            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // TODO Auto-generated method stub        menu.add(0, NEWSONGMENU, 0, "新歌");        menu.add(1, TOOLMENU, 5, "工具");        menu.add(3, ABOUTMENU, 10, "关于");        return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // TODO Auto-generated method stub        switch (item.getItemId()) {        case NEWSONGMENU:            updateSongs();            break;        case TOOLMENU:            // 现在不实现            break;        case ABOUTMENU:            getAlertDialog(MainActivity.this, "关于",                    "本款小软件由mojia独立完成,灰常不错哦,鼓掌!", "好嘞");            break;        default:            break;        }        return super.onOptionsItemSelected(item);    }    private void updateSongs() {        // TODO Auto-generated method stub        FileUtils fileUtils = new FileUtils();        mp3Files = fileUtils.getMp3Files("mp3");        if (mp3Files != null) {            ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();            for (Iterator<Mp3> i = mp3Files.iterator(); i.hasNext();) {                Mp3 mp3 = i.next();                HashMap<String, String> hashMap = new HashMap<String, String>();                hashMap.put("mp3Name", mp3.getName());                list.add(hashMap);            }            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,                    R.layout.songslist, new String[] { "mp3Name" },                    new int[] { R.id.mp3Name });            listView.setAdapter(adapter);        } else {            getAlertDialog(MainActivity.this, "Sorry", "对不起现在还没有歌曲哦,亲", "OK");        }    }    public void getAlertDialog(Context context, String title, String message,            String str_ok) {        // TODO Auto-generated method stub        new AlertDialog.Builder(context).setTitle(title).setMessage(message)                .setPositiveButton(str_ok, new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        // TODO Auto-generated method stub                        finish();                    }                }).show();    }} 


其对应的布局文件main.xml
XML code
<?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="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <ListView            android:id="@+id/listView"            android:layout_width="fill_parent"            android:layout_height="fill_parent" />    </LinearLayout></LinearLayout>


布局文件中的每个item的布局文件:songlist.xml
XML code
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/mp3Name"        android:layout_width="300dip"        android:layout_height="35dip"        android:paddingBottom="1dip"        android:paddingLeft="4dip"        android:paddingTop="1dip"        android:textSize="18dip" /></RelativeLayout>

最后的播放PlayerActivity:
Java code
package wangxin.Activity;import wangxin.mode.Mp3;import android.app.Activity;import android.app.ListActivity;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.Toast;public class PlayerActivity extends Activity {    private ImageButton startButton = null;    private ImageButton stopButton = null;    private MediaPlayer mediaPlayer = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.player);                        Intent intent = getIntent();        Mp3 mp3 = (Mp3) intent.getSerializableExtra("mp3Song");        startButton = (ImageButton) findViewById(R.drawable.start);        startButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Toast.makeText(PlayerActivity.this, "要命啊", Toast.LENGTH_SHORT)                        .show();            }        });        stopButton = (ImageButton) findViewById(R.drawable.stop);    }}

其对应的布局文件player.xml:
XML code
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"    android:gravity="center_horizontal"    android:paddingTop="30dip"    >    <ImageButton        android:id="@+id/startButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/start" />    <ImageButton        android:id="@+id/stopButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/stop"        android:layout_toRightOf="@+id/startButton" /></RelativeLayout> 



问题的异常代码:
11-18 04:52:23.779: W/dalvikvm(434): threadid=1: thread exiting with uncaught exception (group=0x40014760)
11-18 04:52:23.808: E/AndroidRuntime(434): FATAL EXCEPTION: main
11-18 04:52:23.808: E/AndroidRuntime(434): java.lang.RuntimeException: Unable to start activity ComponentInfo{wangxin.Activity/wangxin.Activity.PlayerActivity}: java.lang.NullPointerException
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.os.Looper.loop(Looper.java:126)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.ActivityThread.main(ActivityThread.java:3997)
11-18 04:52:23.808: E/AndroidRuntime(434): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 04:52:23.808: E/AndroidRuntime(434): at java.lang.reflect.Method.invoke(Method.java:491)
11-18 04:52:23.808: E/AndroidRuntime(434): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-18 04:52:23.808: E/AndroidRuntime(434): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-18 04:52:23.808: E/AndroidRuntime(434): at dalvik.system.NativeStart.main(Native Method)
11-18 04:52:23.808: E/AndroidRuntime(434): Caused by: java.lang.NullPointerException
11-18 04:52:23.808: E/AndroidRuntime(434): at wangxin.Activity.PlayerActivity.onCreate(PlayerActivity.java:32)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-18 04:52:23.808: E/AndroidRuntime(434): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
11-18 04:52:23.808: E/AndroidRuntime(434): ... 11 more

程序运行之后进入主Activity,我点击菜单“新歌”,就会正常出现歌曲列表。但是当我点击其中的任何一首歌曲的时候就会报上面的异常。但是如果我把PlayerActivity中的监听器删除之后,一切都正常(当我点击其中的任何一首歌曲的时候就会进入播放的画面),不报异常。
PS:
我在MainActivity中并没有继承ListActivity,因为那样做的话,也是出现上面一样的异常。我整整看了两个调了两天还没搞出来。真心求助啊。

[解决办法]
空指针啊,大哥,很明显的错误
startButton = (ImageButton) findViewById(R.drawable.start);

应该是R.id.startButton

热点排行