尝试切换活动时强制关闭

时间:2012-07-06 16:37:34

标签: android

我尝试使用按钮切换我的应用中的活动,但它会继续强行关闭。继承我的AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.idiedastyear"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".tabView"
        android:label="@string/title_activity_tab_view" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".calculator"
        android:label="Calculator"  >
    </activity>
</application>

以下是包含按钮的活动:

    package com.idiedastyear;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;


public class tabView extends Activity {

    Button calculator;

    @Override



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_view);
        calculator = (Button)findViewById(R.id.calculatorButton);

        calculator.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent calculatorIntent = new Intent(v.getContext(), calculator.class);
                startActivityForResult(calculatorIntent, 0);
            }
        });
    }




}

如果有人能提供帮助就会很棒!谢谢!

编辑[Logcat]:

  

07-06 17:38:06.683:D / memalloc(25668):ion:映射缓冲区   基数:0x5ce6c000大小:3768320偏移量:0 fd:53 07-06 17:38:08.885:   D / memalloc(25668):ion:映射缓冲区:0x5d304000大小:3768320   偏移量:0 fd:60 07-06 17:38:08.985:D / CLIPBOARD(25668):隐藏剪贴板   启动输入对话框:由其他人完成...! 07-06   17:38:11.738:D / memalloc(25668):ion:映射缓冲区:0x5d69c000   大小:3768320偏移量:0 fd:63 07-06 17:38:12.168:   D / AndroidRuntime(25668):关闭VM 07-06 17:38:12.168:   W / dalvikvm(25668):threadid = 1:线程退出未捕获的异常   (组= 0x40c72a68)07-06 17:38:12.178:E / AndroidRuntime(25668):致命   EXCEPTION:main 07-06 17:38:12.178:E / AndroidRuntime(25668):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.idiedastyear / com.idiedastyear.calculator}:   android.view.InflateException:二进制XML文件行#6:错误   膨胀类Textview 07-06 17:38:12.178:E / AndroidRuntime(25668):     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.app.ActivityThread.access $ 600(ActivityThread.java:128)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.os.Handler.dispatchMessage(Handler.java:99)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.os.Looper.loop(Looper.java:137)07-06 17:38:12.178:   E / AndroidRuntime(25668):at   android.app.ActivityThread.main(ActivityThread.java:4514)07-06   17:38:12.178:E / AndroidRuntime(25668):at   java.lang.reflect.Method.invokeNative(Native Method)07-06   17:38:12.178:E / AndroidRuntime(25668):at   java.lang.reflect.Method.invoke(Method.java:511)07-06 17:38:12.178:   E / AndroidRuntime(25668):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:980)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)07-06   17:38:12.178:E / AndroidRuntime(25668):at   dalvik.system.NativeStart.main(Native Method)07-06 17:38:12.178:   E / AndroidRuntime(25668):引起:android.view.InflateException:   二进制XML文件行#6:错误导致类Textview 07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:739)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.inflate(LayoutInflater.java:489)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.inflate(LayoutInflater.java:396)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.inflate(LayoutInflater.java:352)07-06   17:38:12.178:E / AndroidRuntime(25668):at   com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.app.Activity.setContentView(Activity.java:1835)07-06   17:38:12.178:E / AndroidRuntime(25668):at   com.idiedastyear.calculator.onCreate(calculator.java:20)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.app.Activity.performCreate(Activity.java:4470)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)   07-06 17:38:12.178:E / AndroidRuntime(25668):... 11更多07-06   17:38:12.178:E / AndroidRuntime(25668):引起:   java.lang.ClassNotFoundException:android.view.Textview 07-06   17:38:12.178:E / AndroidRuntime(25668):at   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   java.lang.ClassLoader.loadClass(ClassLoader.java:501)07-06   17:38:12.178:E / AndroidRuntime(25668):at   java.lang.ClassLoader.loadClass(ClassLoader.java:461)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.createView(LayoutInflater.java:552)07-06   17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)   07-06 17:38:12.178:E / AndroidRuntime(25668):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)   07-06 17:38:12.178:E / AndroidRuntime(25668):... 21更多

2 个答案:

答案 0 :(得分:0)

您的代码包含以下内容:

Intent calculatorIntent = new Intent(v.getContext(), calculator.class);

第二个参数应该是活动类名称。您似乎也将计算器实例名称与您正在启动的calculator.class混淆。

我建议如下:

  1. 创建一个名为CalculatorActivity的Activity类。
  2. 确保您已在AndroidManifest.xml中定义了
  3. 使用以下行启动活动:
  4. Intent calculatorIntent = new Intent(getBaseContext(), CalculatorActivity.class);

    另外为了帮助您,请同时提供LogCat输出。

答案 1 :(得分:0)

你不应该使用

 startActivityForResult(calculatorIntent, 0);

而不是以上使用

startActivity(calculatorIntent);

startActivityForResult(calculatorIntent,0);  用于获得您正在开始的活动的结果。如果你只想切换活动,startActivity就足够了。虽然您应该发布logcat以获得更好的答案。