繁体   English   中英

找不到活动意图或无法开始

[英]Activity intent can't be found or start

我有一个具有计数器并将其存储到数据库的应用程序。 我正在尝试通过制作视图布局类和视图按钮来检查查询语句是否正常工作。 但是,当我单击查看按钮时,它被强制关闭,并且日志猫说没有发现活动。 我检查以确保它在清单中,并且确保其拼写正确且大写。

    package com.example.testdatabase;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

int counter;
Button add;
Button sub;
Button save;
Button view;
TextView display;
private SharedPreferences prefs;
private String prefName = "MyPref";
MySQLiteHelper db = new MySQLiteHelper(this);


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    counter = 0;
    add = (Button) findViewById(R.id.button1);
    sub = (Button) findViewById(R.id.button2);
    save = (Button) findViewById(R.id.button3);
    view = (Button) findViewById(R.id.button4);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Counter: " + counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Counter: " + counter);
        }
    });

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            db.add(counter);

        }
    });

    view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i = new Intent("com.example.testdatabase.Sqlview");
            startActivity(i);

        }
    });

}

protected void onPause()
{
    super.onPause();

    prefs = getSharedPreferences(prefName, MODE_PRIVATE);
    SharedPreferences.Editor edit = prefs.edit();

    edit.putInt("counter", counter);
    edit.commit();

}

protected void onResume()
{
    super.onResume();

    prefs = getSharedPreferences(prefName, MODE_PRIVATE);

    counter = prefs.getInt("counter", counter);
    display.setText("Counter: " + counter);


}

}

查看类

    package com.example.testdatabase;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Sqlview extends Activity {

protected void onCreate(Bundle savedInstanceState )
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);
    TextView tv = (TextView) findViewById(R.id.textView1);
    MySQLiteHelper info = new MySQLiteHelper(this);
    String data = info.getData();

    tv.setText(data);



}

}

表现

     <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdatabase"
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.testdatabase.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.testdatabase.Sqlview"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

logcat的

    11-17 17:29:15.394: E/AndroidRuntime(279): FATAL EXCEPTION: main
    11-17 17:29:15.394: E/AndroidRuntime(279): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.testdatabase.Sqlview }
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.app.Activity.startActivityForResult(Activity.java:2817)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.app.Activity.startActivity(Activity.java:2923)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at com.example.testdatabase.MainActivity$4.onClick(MainActivity.java:71)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.view.View.performClick(View.java:2408)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.view.View$PerformClick.run(View.java:8816)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.os.Handler.handleCallback(Handler.java:587)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.os.Handler.dispatchMessage(Handler.java:92)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.os.Looper.loop(Looper.java:123)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at java.lang.reflect.Method.invoke(Method.java:521)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-17 17:29:15.394: E/AndroidRuntime(279):  at dalvik.system.NativeStart.main(Native Method)

您为Intent使用了错误的构造函数。 它应该是 :

view.setOnClickListener(new View.OnClickListener() {    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

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

        }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM