我已经通过stackoverflow彻底搜索了我的问题的解决方案,但是所以我没有成功,所以我必须自己提出这个问题。我的问题是,当我
时Songname.setText(Fullname);
我的应用崩溃了。如果我注释掉代码,那么应用程序不会崩溃。 我的Fullname变量是
Fullname = (artist + ":" + album + ":" + track);
来自BroadcastReceiver:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.v("tag ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.v("tag", artist + ":" + album + ":" + track);
Fullname = (artist + ":" + album + ":" + track);
Toast.makeText(MusicPlayer.this, track, Toast.LENGTH_SHORT).show();
update();
}
};
(我认为)我的TextView设置正确,所以我怀疑这是问题,但我会发布以防万一:
TextView Songname;
TextView Songname = (TextView) findViewById(R.id.musictitle);
以下是整个xml以防万一:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/musictitle"
android:layout_marginTop="46dp"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="New Button"
android:id="@+id/button"
android:layout_alignBottom="@+id/imageView"
android:layout_toLeftOf="@+id/imageView"
android:layout_marginRight="25dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="New Button"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignParentLeft="true"
android:layout_marginLeft="35dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="New Button"
android:id="@+id/button3"
android:layout_alignTop="@+id/textView7"
android:layout_toLeftOf="@+id/textView7" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="New Button"
android:id="@+id/button4"
android:layout_alignTop="@+id/textView7"
android:layout_toRightOf="@+id/seekBar" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView7"
android:gravity="center"
android:layout_gravity="center"
android:layout_alignBottom="@+id/imageView"
android:layout_toLeftOf="@+id/button4" />
<SeekBar
android:layout_width="500dp"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:indeterminate="false"
android:layout_below="@+id/musictitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/imageView"
android:layout_below="@+id/seekBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />
和类文件,如果这是问题:
package com.androidddev101.ep8;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.content.IntentFilter;
import android.widget.TextView;
import android.widget.Toast;
public class MusicPlayer extends Activity implements View.OnClickListener {
public static final String SERVICECMD = "com.android.music.musicservicecommand";
String Fullname;
String artist, album, track;
TextView Songname;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musicplayer);
TextView Songname = (TextView) findViewById(R.id.musictitle);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.playbackcomplete");
iF.addAction("com.android.music.queuechanged");
iF.addAction("com.htc.music.metachanged");
iF.addAction("fm.last.android.metachanged");
iF.addAction("com.sec.android.app.music.metachanged");
iF.addAction("com.nullsoft.winamp.metachanged");
iF.addAction("com.amazon.mp3.metachanged");
iF.addAction("com.miui.player.metachanged");
iF.addAction("com.real.IMP.metachanged");
iF.addAction("com.sonyericsson.music.metachanged");
iF.addAction("com.rdio.android.metachanged");
iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
iF.addAction("com.andrew.apollo.metachanged");
iF.addAction("com.jrtstudio.AnotherMusicPlayer.metachanged");
registerReceiver(mReceiver, iF);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.v("tag ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.v("tag", artist + ":" + album + ":" + track);
Fullname = (artist + ":" + album + ":" + track);
Toast.makeText(MusicPlayer.this, track, Toast.LENGTH_SHORT).show();
update();
}
};
public void update() {
{
// Fullname = (artist + ":" + album + ":" + track);
Log.v("tag", Fullname + "123");
Songname.setText(Fullname);
Songname.setSelected(true);
Songname.setEllipsize(TextUtils.TruncateAt.MARQUEE);
Songname.setSingleLine(true);
}
}
@Override
public void onClick(View v) {
}
}
注意:toast消息有效,并为我提供了曲目名称
以下是Logcat的错误
引起:java.lang.NullPointerException 在com.androidddev101.ep8.MusicPlayer.update(MusicPlayer.java:81) 在com.androidddev101.ep8.MusicPlayer $ 1.onReceive(MusicPlayer.java:72)
感谢您提供任何帮助。
答案 0 :(得分:0)
TextView Songname; // not instantiated for global use
TextView Songname = (TextView) findViewById(R.id.musictitle); // instantiated in only oncreate
将其更改为此
TextView Songname;
Songname = (TextView) findViewById(R.id.musictitle);
答案 1 :(得分:0)
Songname的引用是onCreate的本地引用,您不需要声明另一个名为Songname的变量。
package com.androidddev101.ep8;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.content.IntentFilter;
import android.widget.TextView;
import android.widget.Toast;
public class MusicPlayer extends Activity implements View.OnClickListener {
public static final String SERVICECMD = "com.android.music.musicservicecommand";
String Fullname;
String artist, album, track;
TextView Songname;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musicplayer);
Songname = (TextView) findViewById(R.id.musictitle);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.playbackcomplete");
iF.addAction("com.android.music.queuechanged");
iF.addAction("com.htc.music.metachanged");
iF.addAction("fm.last.android.metachanged");
iF.addAction("com.sec.android.app.music.metachanged");
iF.addAction("com.nullsoft.winamp.metachanged");
iF.addAction("com.amazon.mp3.metachanged");
iF.addAction("com.miui.player.metachanged");
iF.addAction("com.real.IMP.metachanged");
iF.addAction("com.sonyericsson.music.metachanged");
iF.addAction("com.rdio.android.metachanged");
iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
iF.addAction("com.andrew.apollo.metachanged");
iF.addAction("com.jrtstudio.AnotherMusicPlayer.metachanged");
registerReceiver(mReceiver, iF);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.v("tag ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.v("tag", artist + ":" + album + ":" + track);
Fullname = (artist + ":" + album + ":" + track);
Toast.makeText(MusicPlayer.this, track, Toast.LENGTH_SHORT).show();
update();
}
};
public void update() {
{
// Fullname = (artist + ":" + album + ":" + track);
Log.v("tag", Fullname + "123");
Songname.setText(Fullname);
Songname.setSelected(true);
Songname.setEllipsize(TextUtils.TruncateAt.MARQUEE);
Songname.setSingleLine(true);
}
}
@Override
public void onClick(View v) {
}
}