gpt4 book ai didi

java - 从 TextView 设置文本时 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 12:00:35 25 4
gpt4 key购买 nike

我已经通过 stackoverflow 彻底搜索了我的问题的解决方案,但是我没有成功,所以我必须自己提出这个问题。我的问题是当我

Songname.setText(Fullname);

我的应用程序崩溃了。如果我注释掉代码,那么应用程序就不会崩溃。我的全名变量是

Fullname = (artist + ":" + album + ":" + track);

来自广播接收器:

    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 的错误

Caused by: java.lang.NullPointerException at com.androidddev101.ep8.MusicPlayer.update(MusicPlayer.java:81) at com.androidddev101.ep8.MusicPlayer$1.onReceive(MusicPlayer.java:72)

感谢您提供的任何帮助。

最佳答案

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);

关于java - 从 TextView 设置文本时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28004409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com