gpt4 book ai didi

android - MediaPlayer和 fragment -java.lang.IllegalStateException:无法执行 Activity 的方法

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

我已经设置了MainActivity,其中包含多个选项卡,可以通过滑动来导航。因此,每当我滑动到相应的选项卡时,一个片段就会显示以XML格式描述的imageview,并且在点击该imageview时可以启用音频播放。

imageview是在frag.xml中使用相应的android:onClick属性设置的。

因此,为简单起见,我们考虑1个片段(连接到frag.xml)和1个主要 Activity (连接到main.xml)。

现在,每当我点击imageview时,该应用程序都不会播放任何声音,但会立即崩溃,并显示以下logcat报告:

01-19 15:36:29.211: E/AndroidRuntime(20718): FATAL EXCEPTION: main
01-19 15:36:29.211: E/AndroidRuntime(20718): java.lang.IllegalStateException: Could not execute method of the activity
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.view.View$1.onClick(View.java:3640)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.view.View.performClick(View.java:4247)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.view.View$PerformClick.run(View.java:17728)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.os.Handler.handleCallback(Handler.java:730)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.os.Looper.loop(Looper.java:137)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.app.ActivityThread.main(ActivityThread.java:5289)
01-19 15:36:29.211: E/AndroidRuntime(20718): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 15:36:29.211: E/AndroidRuntime(20718): at java.lang.reflect.Method.invoke(Method.java:525)
01-19 15:36:29.211: E/AndroidRuntime(20718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
01-19 15:36:29.211: E/AndroidRuntime(20718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-19 15:36:29.211: E/AndroidRuntime(20718): at dalvik.system.NativeStart.main(Native Method)
01-19 15:36:29.211: E/AndroidRuntime(20718): Caused by: java.lang.reflect.InvocationTargetException
01-19 15:36:29.211: E/AndroidRuntime(20718): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 15:36:29.211: E/AndroidRuntime(20718): at java.lang.reflect.Method.invoke(Method.java:525)
01-19 15:36:29.211: E/AndroidRuntime(20718): at android.view.View$1.onClick(View.java:3635)
01-19 15:36:29.211: E/AndroidRuntime(20718): ... 11 more
01-19 15:36:29.211: E/AndroidRuntime(20718): Caused by: java.lang.NullPointerException
01-19 15:36:29.211: E/AndroidRuntime(20718): at com.example.MainActivity.hellofunction(MainActivity.java:63)
01-19 15:36:29.211: E/AndroidRuntime(20718): ... 14 more
01-19 15:36:30.522: I/Process(20718): Sending signal. PID: 20718 SIG: 9

frag.xml-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollyClips"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="403dp"
android:layout_marginLeft="1dp"
android:background="@color/black"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="right"
android:src="@drawable/ico" />

<ImageView
android:id="@+id/ImageView01"
android:contentDescription="solhello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ico"
android:onClick="hellofunction"/>

</RelativeLayout>

</ScrollView>

我的Java代码如下-

MainActivity.java:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter adapter;
private ActionBar actionbar;

private String[] tabs={"A"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//Initialize
viewPager=(ViewPager)findViewById(R.id.pager);
actionbar=getActionBar();
adapter=new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(adapter);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//Add tabs
for(String tab_name:tabs)
{
actionbar.addTab(actionbar.newTab().setText(tab_name)
.setTabListener(this));
}
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}

public void hellofunction(View v)
{ mp.release();
mp=MediaPlayer.create(this, R.raw.helloworld);
mp.start();
}

}

ClipsFragment.java:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ClipsFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.frag, container, false);

return rootView;

}
}

我究竟做错了什么?如何解决此问题并使音频播放正常?

最佳答案

您甚至在初始化mp之前就调用release

 mp.release();
mp=MediaPlayer.create(this, R.raw.helloworld);

编辑:

片段中
 MediaPlayer mp; 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.frag, container, false);
ImageView iv = (ImageView) rootView.findViewById(R.id.ImageView01);
iv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mp=MediaPlayer.create(getActivity(), R.raw.helloworld);
mp.start():
}
});
return rootView;

}

您可以删除 android:onClick="hellofunction"

关于android - MediaPlayer和 fragment -java.lang.IllegalStateException:无法执行 Activity 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21214974/

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