无法启动活动显示您是否已声明此活动

时间:2017-03-26 09:03:26

标签: java android android-recyclerview

package com.acer.syncmysql;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
    public static final int CONNECTION_TIMEOUT = 10000;
    public static final int READ_TIMEOUT = 15000;
    private RecyclerView mRVFishPrice;
    private AdapterFish mAdapter;
    private DBHelper mydb;
    View ChildView ;
    int RecyclerViewItemPosition ;
    private DBcheckHelper dch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Make call to AsyncTask
        new AsyncLogin().execute();

        mydb = new DBHelper(this);
        dch=new DBcheckHelper(this);
    }

    private class AsyncLogin extends AsyncTask<String, String, String> {
        ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
        HttpURLConnection conn;
        URL url = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread
            pdLoading.setMessage("\tLoading...");
            pdLoading.setCancelable(false);
            pdLoading.show();

        }

        @Override
        protected String doInBackground(String... params) {
            try {

                // Enter URL address where your json file resides
                // Even you can make call to php file which returns json data
                url = new URL("http://mycollege.eu5.org/example.json");

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.toString();
            }
            try {

                // Setup HttpURLConnection class to send and receive data from php and mysql
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestMethod("GET");

                // setDoOutput to true as we recieve data from json file
                conn.setDoOutput(true);

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return e1.toString();
            }

            try {

                int response_code = conn.getResponseCode();

                // Check if successful connection made
                if (response_code == HttpURLConnection.HTTP_OK) {

                    // Read data sent from server
                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    StringBuilder result = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }

                    // Pass data to onPostExecute method
                    return (result.toString());

                } else {

                    return ("unsuccessful");
                }

            } catch (IOException e) {
                e.printStackTrace();
                return e.toString();
            } finally {
                conn.disconnect();
            }


        }

        @Override
        protected void onPostExecute(String result) {

            //this method will be running on UI thread

            pdLoading.dismiss();
            List<DataFish> data=new ArrayList<>();

            pdLoading.dismiss();
            try {

                JSONArray jArray = new JSONArray(result);

                // Extract data from json and store into ArrayList as class objects
                for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    int last=mydb.getlast();
                    last++;
                    int nr= json_data.getInt("price");
                    if(mydb.norepeat(nr)==2) {


                        String Fi = json_data.getString("fish_img");
                        String Fn = json_data.getString("fish_name");
                        String cn = json_data.getString("cat_name");
                        String sn = json_data.getString("size_name");
                        int pr = json_data.getInt("price");

                        if (mydb.insertContact(last, Fi, Fn,
                                cn, sn,
                                pr)) {
                        } else {
                            Toast.makeText(getApplicationContext(), "not done",
                                    Toast.LENGTH_SHORT).show();
                        }


                    }



                }

                for(int i=1;i<=mydb.getlast();i++) {
                    DataFish fishData = new DataFish();
                    fishData.fishImage = mydb.getname(i);
                    fishData.fishName = mydb.getphone(i);
                    fishData.catName = mydb.getemail(i);
                    fishData.sizeName = mydb.getplace(i);
                    fishData.price = i;
                    data.add(fishData);
                }
                mRVFishPrice = (RecyclerView)findViewById(R.id.fishPriceList);
                mAdapter = new AdapterFish(MainActivity.this, data);
                mRVFishPrice.setAdapter(mAdapter);
                mRVFishPrice.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                mRVFishPrice.setItemAnimator(new DefaultItemAnimator());
                mRVFishPrice.setAdapter(mAdapter);

                mRVFishPrice.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRVFishPrice, new RecyclerTouchListener.ClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        dch.updateContact(position + 1);

                        try {
                            Intent intent = new Intent(MainActivity.this,DetailsActivity.class);
                            startActivity(intent);;
                        }
                        catch (Exception e){
                            Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onLongClick(View view, int position) {

                    }
                }));


            } catch (JSONException e) {


                for(int i=1;i<=mydb.getlast();i++) {
                    DataFish fishData = new DataFish();
                    fishData.fishImage = mydb.getname(i);
                    fishData.fishName = mydb.getphone(i);
                    fishData.catName = mydb.getemail(i);
                    fishData.sizeName = mydb.getplace(i);
                    fishData.price = i;
                    data.add(fishData);
                }
                mRVFishPrice = (RecyclerView)findViewById(R.id.fishPriceList);
                mAdapter = new AdapterFish(MainActivity.this, data);
                mRVFishPrice.setAdapter(mAdapter);
                mRVFishPrice.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                mRVFishPrice.setItemAnimator(new DefaultItemAnimator());
                mRVFishPrice.setAdapter(mAdapter);
                mRVFishPrice.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRVFishPrice, new RecyclerTouchListener.ClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        dch.updateContact(position+1);

                        Toast.makeText(getApplicationContext(), dch.getlast()+ " is selected!", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onLongClick(View view, int position) {

                    }
                }));


                try{
                    int i=mydb.getlast();
                    String strI = String.valueOf(mydb.getprice(1));
                    Toast.makeText(MainActivity.this,strI, Toast.LENGTH_LONG).show();


                }
                catch (Exception e1){
                    Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
                }

            }

        }

    }
}

我想从mainactivity开始另一个活动,它显示一些错误,我也搜索了类似的问题,但无法解决它。 这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

问题出在这里

mRVFishPrice = (RecyclerView)findViewById(R.id.fishPriceList);
            mAdapter = new AdapterFish(MainActivity.this, data);
            mRVFishPrice.setAdapter(mAdapter);
            mRVFishPrice.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            mRVFishPrice.setItemAnimator(new DefaultItemAnimator());
            mRVFishPrice.setAdapter(mAdapter);

            mRVFishPrice.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRVFishPrice, new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, int position) {
                    dch.updateContact(position + 1);

                    try {
                        Intent intent = new Intent(MainActivity.this,DetailsActivity.class);
                        startActivity(intent);;
                    }
                    catch (Exception e){
                        Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));

我找不到任何解决方案,任何人都有助于解决这个问题

3 个答案:

答案 0 :(得分:1)

你有一个拼写错误:加倍Semicolon ..

使用它:

 Intent intent = new Intent(MainActivity.this,DetailsActivity.class);
 startActivity(intent);  //remove double semicolon

在清单中:声明活动类:

<activity android:name=".DetailsActivity" />

还要清理构建:转到Build-->Clean Project-->Run

答案 1 :(得分:0)

首先检查您是否在同一个包中声明了两个活动。如果没有,则在清单文件中用完整的包名称声明它

<强>像:

 <activity android:name="yourPackage.DetailsActivity" />

答案 2 :(得分:0)

也许你可以通过它的包名开始活动。

setClassName(String packageName, String className)

或在你的情况下

Intent intent = new Intent();
intent.setClassName("com.acer.syncmysql","com.acer.syncmysql.DetailsActivity"); 
startActivity(intent);