2014-02-25 88 views
-2

我正在爲Android編寫一個switch語句,並且在第一個位置遇到問題。雖然其他職位工作正常。我一直在嘗試幾個小時,但仍然沒有看到問題。但是,檢查Logcat我意識到它與main.xml有關。 這是我的main.xml:應用程序(名稱)已意外停止,請再試一次

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

    <ImageView 
     android:id="@+id/tech_g" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="1dp" 
     android:contentDescription="@string/smartphone" 
     android:src="@drawable/pic_g" /> 

    <TextView 
     android:id="@+id/tech_g2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="25sp" 
     android:textColor="#FFFFFF" /> 
</LinearLayout> 

這裏是java:

package net.android.nameapp; 
public class Smartphone extends Activity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.smartphone); 
     Button btn1 = (Button) findViewById(R.id.btnSmartphone); 
     btn1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       startActivity (new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://www.vw/smartphone?&zipRdr=y"))); 
      } 
     }); 
     } 
} 

我已經在清單文件中定義我所有的類。這裏是logcat的說什麼:

01-12 10:34:32.743: E/AndroidRuntime(2668): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.android.nameapp/net.android.nameapp.Smartphone}: java.lang.ClassCastException: android.widget.ImageView 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.os.Looper.loop(Looper.java:123) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at dalvik.system.NativeStart.main(Native Method) 
01-12 10:34:32.743: E/AndroidRuntime(2668): Caused by: java.lang.ClassCastException: android.widget.ImageView 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at net.android.laptechgadgetsapp.Smartphone.onCreate(Smartphone.java:15) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
01-12 10:34:32.743: E/AndroidRuntime(2668):  ... 11 more 

這裏是java:

package net.android.nameapp; 
public class Smartphone extends Activity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.smartphone); 
     Button btn1 = (Button) findViewById(R.id.btnSmartphone); 
     btn1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       startActivity (new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://www.vw/smartphone?&zipRdr=y"))); 
      } 
     }); 
     } 
} 

package net.android.nameapp; 
public class Main extends ListActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String[] gdgt={"Smartphone", "Laptop", "iPod","iPad", "Smartpen"}; 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.tech_g2, gdgt)); 
     } 
     protected void onListItemClick(ListView l, View v, int position, long id){ 
      switch(position){ 
      case 0: 
       startActivity(new Intent(Main.this, Smartphone.class)); 
       break; 

能否請您在此提供一些線索?

+0

是的所有活動都在清單文件中聲明。 – stdntmp

+0

好吧,你們都是代碼非常長。我發佈一個與錯誤有關的信息。此應用程序正在運行,但該案例的第一個位置除外。所以不是這樣,因爲所有其他職位都能正常工作。 – stdntmp

+0

是R.id.btnSmartphone按鈕還是imageview? – Amrut

回答

1

//你smartphone.xml您聲明ImageView並在Smartphone活動您所呼叫的按鈕

更改它的ImageView

Button btn1 = (Button) findViewById(R.id.btnSmartphone); 

ImageViewbtn1 = (ImageView) findViewById(R.id.btnSmartphone); 
0

如果你的代碼是好的然後刪除你的R文件。 (別擔心它會再次自動生成)。基本上任何讓你的項目重建並重新生成R文件的東西。有時候Eclipse往往會搞亂資源。這會導致一些奇怪的行爲,例如classCastException(s),這些行爲在Eclipse切換視圖的ID時發生。

相關問題