2013-03-25 62 views
0

我從我的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); 
     } 
    }); 
+1

發佈帶有錯誤的堆棧跟蹤。 – sebster 2013-03-25 22:00:29

回答

1

您不能啓動像的活性的片段:

new Intent(MainActivity.this, ButtonFragment1.class); 

你需要獲取FragmentManager的一個實例,然後調用add(),更換()`等使用活動中的碎片。

開發人員指南在此主題上有多個詳細的examplestutorials


你也應該刪除錯誤的聲明中您的清單,因爲你現在知道,一個片段是不是一個活動:

<activity android:name=".ButtonFragment1"></activity> 


會怎樣我爲每個按鈕獲取活動類和FragmentManager類?

老實說,我不明白你想用你的ButtonFragments做什麼,所以我不能幫你做具體事情,但我可以指出你在正確的方向。裏面的活動(或FragmentActivity),當你想創建你片段使用的副本:

ButtonFragment buttonFrag = new ButtonFragment(); 
// Use setArguments() to pass extras to your Fragment 
getFragmentManager().beginTransaction().add(R.id.fragment_container, buttonFrag).commit(); 

這是一個片段添加到您的佈局的正確方法。

+0

我將如何獲取每個按鈕的活動類和FragmentManager類? – DuckiesUnite 2013-03-25 22:26:19

+0

當我按下按鈕進行下一個佈局/活動時,我正從上一個活動中獲取動畫。另外,當我點擊後退按鈕時,它將我帶到上一個活動。我想彌補這一點,所以我做了一些研究,發現碎片會糾正它。對不起,爲了麻煩,我是一名學生,作爲我的一個班級項目工作。 – DuckiesUnite 2013-03-25 22:44:32

+0

爲什麼在'startActivity()'之前調用'setContentView()'?這將改變你即將離開的活動的佈局......無論如何,雖然碎片很適合「劃分」你的活動,但你可以通過代碼來改變從一個活動到另一個活動的轉換[像這樣。](http:// stackoverflow.com/q/3389501/1267661) – Sam 2013-03-26 00:26:01