应用程序挂在按钮的onClickListener中

时间:2013-11-19 12:34:06

标签: android onclicklistener

每次点击我创建的按钮时,我的应用程序都会挂起。我试图将两个Java代码分开,但它们运行良好。有谁可以帮我检查一下?我只想通过onClickListener调用第二个编码。

public class MainActivity extends Activity{
    TextView aziangle,elevation,numberof,rightasc,decli,hourangle,solartime;
    Button goedit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();   
    }

    private void initialize() {
        // TODO Auto-generated method stub
        aziangle=(TextView)findViewById(R.id.tvAziAngle);
        elevation=(TextView)findViewById(R.id.tvElevation);
        numberof=(TextView)findViewById(R.id.tvNumberof);
        rightasc=(TextView)findViewById(R.id.tvRightAsc);
        decli=(TextView)findViewById(R.id.tvDecli);
        hourangle=(TextView)findViewById(R.id.tvHourAngle);
        solartime=(TextView)findViewById(R.id.tvSolarTime);
        goedit=(Button)findViewById(R.id.bedit);

        goedit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch(v.getId()) 
                {
                case R.id.bedit:
                    Intent ed=new Intent(MainActivity.this, Menu.class);
                    startActivity(ed);
                    break;
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

public class Menu extends ListActivity {
    String selection[]={"Latitude","Longtitude","TimeZone","DaylightSavingTime","OffsetParameters"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, selection));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String choosen= selection[position];
    try{
        Class ourclass = Class.forName("com.example.suntracking" + choosen);
        Intent ourintent=new Intent(Menu.this,ourclass);
        startActivity(ourintent);
    }catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
}
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.suntracking"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.suntracking.MainActivity"
            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="com.example.suntracking.Menu"
            android:label="@string/app_name" >

        </activity>
    </application>

</manifest>
编辑:我再次检查,我很确定问题出现在OnClickListener中,因为当我删除那些我可以按下按钮时它不会挂起,虽然它也不会转到其他页面。

11-19 21:48:27.262: D/TextLayoutCache(9308): Enable myanmar Zawgyi converter
11-19 21:48:27.262: D/TextLayoutCache(9308): Enable myanmar Zawgyi converter
11-19 21:48:27.262: D/TextLayoutCache(9308): Enable myanmar Zawgyi converter
11-19 21:48:27.292: D/libEGL(9308): loaded /vendor/lib/egl/libEGL_adreno.so
11-19 21:48:27.292: D/libEGL(9308): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
11-19 21:48:27.292: D/libEGL(9308): loaded /vendor/lib/egl/libGLESv2_adreno.so
11-19 21:48:27.292: I/Adreno-EGL(9308): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
11-19 21:48:27.292: I/Adreno-EGL(9308): OpenGL ES Shader Compiler Version: 17.01.10.SPL
11-19 21:48:27.292: I/Adreno-EGL(9308): Build Date: 09/26/13 Thu
11-19 21:48:27.292: I/Adreno-EGL(9308): Local Branch: 
11-19 21:48:27.292: I/Adreno-EGL(9308): Remote Branch: 
11-19 21:48:27.292: I/Adreno-EGL(9308): Local Patches: 
11-19 21:48:27.292: I/Adreno-EGL(9308): Reconstruct Branch: 
11-19 21:48:27.342: D/OpenGLRenderer(9308): Enabling debug mode 0
11-19 21:48:27.372: D/TextLayoutCache(9308): Enable myanmar Zawgyi converter
11-19 21:48:27.372: D/TextLayoutCache(9308): Enable myanmar Zawgyi converter
11-19 21:48:27.372: D/TextLayoutCache(9308): Enable myanmar Zawgyi converter
11-19 21:48:28.253: D/AndroidRuntime(9308): Shutting down VM
11-19 21:48:28.253: W/dalvikvm(9308): threadid=1: thread exiting with uncaught exception (group=0x418f9898)
11-19 21:48:28.253: E/AndroidRuntime(9308): FATAL EXCEPTION: main
11-19 21:48:28.253: E/AndroidRuntime(9308): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.suntracking/android.view.Menu}; have you declared this activity in your AndroidManifest.xml?
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1645)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1441)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.app.Activity.startActivityForResult(Activity.java:3464)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.app.Activity.startActivityForResult(Activity.java:3425)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at com.example.suntracking.MainActivity.onClick(MainActivity.java:47)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.view.View.performClick(View.java:4475)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.view.View$PerformClick.run(View.java:18786)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.os.Handler.handleCallback(Handler.java:730)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.os.Looper.loop(Looper.java:137)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at android.app.ActivityThread.main(ActivityThread.java:5419)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at java.lang.reflect.Method.invoke(Method.java:525)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-19 21:48:28.253: E/AndroidRuntime(9308):     at dalvik.system.NativeStart.main(Native Method)

抱歉,我真的不知道如何发布logcat,这些是我在程序的logcat中看到的东西。

这是MainActivity xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="95">

    <TextView
        android:id="@+id/tvDay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />


    <TextView
        android:id="@+id/tvAziAngle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvElevation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvNumberof"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvRightAsc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvDecli"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvHourAngle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvSolarTime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:text="TextView" />

    </LinearLayout>

    <Button
        android:id="@+id/bedit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Edit" 
        android:layout_weight="5"/>
</LinearLayout>

我也试过让第二个活动像只有setcontent视图一样简单,但仍然会出现同样的问题。

1 个答案:

答案 0 :(得分:0)

菜单已经是android

使用的类名
android.view.Menu

尝试将您的第二个活动名称替换为其他名称。 我希望它能解决你的问题。