簡體   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