应用程序(名称)意外停止,请再试一次

时间:2014-02-25 06:12:40

标签: android

我正在为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")));
            }
        });
        }
}

我在Manifest文件中定义了所有类。这是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;

请你在这里说清楚一点?

2 个答案:

答案 0 :(得分:1)

//在您的smartphone.xml中声明ImageView以及您正在调用的Smartphone活动按钮

将其更改为ImageView

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

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

答案 1 :(得分:0)

如果您的代码没问题,请删除您的R文件。 (不要担心它会再次自动生成)。基本上任何使您的项目重建并重新生成R文件的东西。 有时Eclipse往往会弄乱资源。这会导致一些奇怪的行为,例如classCastException(s),当Eclipse切换你的视图时会发生这种行为。 ids周围。