我从buttonfragment.class获得运行时异常。我做了一个零碎的按钮栏,每个按钮都有自己的布局和活动。我一直在改变一些事情,无法弄清楚片段类中的错误在哪里。感谢
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
btn1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i = new Intent(MainActivity.this, ButtonFragment1.class);
setContentView(R.layout.activity_main);
startActivity(i);
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i = new Intent(MainActivity.this, ButtonFragment2.class);
i.setAction(Intent.ACTION_VIEW);
i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.agil_main);
startActivity(i);
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i = new Intent(MainActivity.this, ButtonFragment3.class);
i.setAction(Intent.ACTION_VIEW);
i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.int_main);
startActivity(i);
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i = new Intent(MainActivity.this, ButtonFragment4.class);
i.setAction(Intent.ACTION_VIEW);
i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.misc_main);
startActivity(i);
}
});
}}
ButtonFragment1.java
public class ButtonFragment1 extends Fragment implements OnClickListener
{
private static final String TAG = null;
protected static final Context context=null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.activity_main, container, false);
Button Btn = (Button) view.findViewById(R.id.btn1);
Btn.setOnClickListener(this);
return view;
}
public void onClick(View v)
{
if(v.getId()==R.id.btn1)
{
// TODO Auto-generated method stub
startActivity(new Intent(v.getContext(),MainActivity.class));
}
}
}
buttonbar.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:shrinkColumns="*"
android:stretchColumns="*"
android:background="#6B1414">
<TableRow
android:id="@+id/tableRow1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="@+id/btn1"
android:text="@string/Str"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="@+id/btn2"
android:text="@string/Agl"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="@+id/btn3"
android:text="@string/Int"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="@+id/btn4"
android:text="@string/Misc"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:padding="18dip"/>
</TableRow>
</TableLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.killerphone.dota2sounds"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name="com.killerphone.dota2sounds.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainAgil"></activity>
<activity android:name=".MainInt"></activity>
<activity android:name=".MainMisc"></activity>
<activity android:name=".ButtonFragment1"></activity>
<activity android:name=".ButtonFragment2"></activity>
<activity android:name=".ButtonFragment3"></activity>
<activity android:name=".ButtonFragment4"></activity>
</application>
更新:发布跟踪错误 - 我可以进入下一个布局/活动,但之后它会立即崩溃。
03-25 15:08:16.705: D/AndroidRuntime(3438): Shutting down VM
03-25 15:08:16.705: W/dalvikvm(3438): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
03-25 15:08:16.705: E/AndroidRuntime(3438): FATAL EXCEPTION: main
03-25 15:08:16.705: E/AndroidRuntime(3438): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.killerphone.dota2sounds/com.example.test.ButtonFragment2}: java.lang.ClassCastException: com.example.test.ButtonFragment2
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1849)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1960)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.ActivityThread.access$1500(ActivityThread.java:145)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1045)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.os.Looper.loop(Looper.java:150)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.ActivityThread.main(ActivityThread.java:4369)
03-25 15:08:16.705: E/AndroidRuntime(3438): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:08:16.705: E/AndroidRuntime(3438): at java.lang.reflect.Method.invoke(Method.java:507)
03-25 15:08:16.705: E/AndroidRuntime(3438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:846)
03-25 15:08:16.705: E/AndroidRuntime(3438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
03-25 15:08:16.705: E/AndroidRuntime(3438): at dalvik.system.NativeStart.main(Native Method)
03-25 15:08:16.705: E/AndroidRuntime(3438): Caused by: java.lang.ClassCastException: com.example.test.ButtonFragment2
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-25 15:08:16.705: E/AndroidRuntime(3438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1841)
03-25 15:08:16.705: E/AndroidRuntime(3438): ... 11 more
03-25 15:08:16.806: D/dalvikvm(3438): GC_CONCURRENT freed 234K, 48% free 2974K/5639K, external 0K/0K, paused 3ms+2ms
03-25 15:08:18.157: I/Process(3438): Sending signal. PID: 3438 SIG: 9
在添加片段之前更新#2按钮,但是从之前的布局/活动中获取动画。基本上,按钮在所有布局和活动中放置在应用程序的顶部。
btn1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i = new Intent(getApplicationContext(), MainActivity.class);
i.setAction(Intent.ACTION_VIEW);
i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
startActivity(i);
}
});
答案 0 :(得分:1)
您无法像活动一样启动片段:
new Intent(MainActivity.this, ButtonFragment1.class);
击> <击> 撞击>
您需要获取FragmentManager的实例,然后调用add()
,替换()`等,以在您的Activity中使用Fragments。
“开发人员指南”中有关于此主题的多个详细examples和tutorials。
您还应该删除清单中的错误声明,因为您现在知道片段不是活动:
<activity android:name=".ButtonFragment1"></activity>
击> <击> 撞击>
我如何为每个按钮获取活动类和FragmentManager类?
老实说,我不明白你想用ButtonFragments做什么,所以我不能帮助你解决具体问题,但我可以指出你正确的方向。在您的Activity(或FragmentActivity)中,当您想要创建Fragment的副本时使用:
ButtonFragment buttonFrag = new ButtonFragment();
// Use setArguments() to pass extras to your Fragment
getFragmentManager().beginTransaction().add(R.id.fragment_container, buttonFrag).commit();
这是将片段添加到布局的正确方法。