我從我的buttonfragment.class中獲取運行時異常。我做了一個零碎的按鈕欄,每個按鈕都有自己的佈局和活動。我一直在改變事情,無法弄清楚片段類中的錯誤在哪裏。由於ButtonFragment類中的RunTimeException
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
Update#2添加Fragment之前的按鈕,但正在從之前的佈局/活動中獲取動畫。基本上,按鈕在所有佈局和活動中都放置在應用程序的頂部。
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);
}
});
發佈帶有錯誤的堆棧跟蹤。 – sebster 2013-03-25 22:00:29