【问题标题】:After Click Button do not open the new activity单击按钮后不要打开新活动
【发布时间】:2014-08-10 13:04:53
【问题描述】:

单击按钮后,我在启动新活动时遇到了一些问题,我已经检查了数百次代码,但我没有发现我认为是清单中的错误。

你能帮帮我吗?

清单信息:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.productioncontrolinventory"
    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.productioncontrolinventory.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.productioncontrolinventory.Editacountentry" android:label="@string/app_name">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity><activity android:name="com.example.productioncontrolinventory.Inputinventory" android:label="@string/app_name">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity></application>

</manifest>

MainActivity 代码:

case R.id.bInputInventory:
                //Go to Input screen
                try{
                    Class ourClass = Class.forName("com.example.productioncontrolinventory.Inputinventory");
                    Intent ourIntent = new Intent(MainActivity.this, ourClass);
                    startActivity(ourIntent);
                 }catch(ClassNotFoundException g){
                        g.printStackTrace();
                        }
                break;

谢谢

【问题讨论】:

  • 请分享您的堆栈跟踪!我们如何知道异常是否被抛出。提供您的 logcat 输出。
  • 清空就像按钮不工作,但其他按钮具有其他功能的工作......同样的问题延伸到整个应用程序,所以我认为应该与清单有关

标签: android button android-activity manifest


【解决方案1】:

你可以试试

Class ourClass = Class.forName("com.example.productioncontrolinventory.Inputinventory");
Intent ourIntent = new Intent(getApplicationContext(), ourClass);
startActivity(ourIntent);

或者干脆

Intent ourIntent = new Intent(getApplicationContext(), Inputinventory.class);
startActivity(ourIntent);

而不是

Class ourClass = Class.forName("com.example.productioncontrolinventory.Inputinventory");
Intent ourIntent = new Intent(MainActivity.this, ourClass);
startActivity(ourIntent);

如果这些都不起作用,您应该通过任何其他命令(如 toast 通知等)检查您的按钮是否正常工作。

您的清单文件看起来不错。

【讨论】:

    【解决方案2】:

    你为什么不简单地使用:

    Intent i = new Intent(MainActivity.this , Inputinventory.class );
    startActivity(i);
    

    如果 "Inputinventory.java" 扩展了 Fragment ,请以这种方式表达您的意图:

    Context context;
    Intent intent = new Intent(context ,Inputinventory.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    

    注意:第二个条件,必须在构造函数中初始化上下文变量!

    【讨论】:

      【解决方案3】:

      您可以使用以下代码:

      {
      Button btn;
      btn = (Button) findViewbyId(R.id.bInputInventory);
      btn.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent ourIntent = new Intent(this, Inputinventory.class);
                  startActivity(ourIntent);
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-25
        • 2017-12-03
        • 2013-04-10
        • 2019-03-07
        • 2011-11-28
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多