当我检查断点时,onCreate 没有被调用

onCreate not getting called when i checked with breakpoints

onCreate 没有被调用,用断点调试代码。

public class request_tmdb extends AppCompatActivity {

public request_tmdb() {
    new request_token().execute();
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startActivity(new Intent(this,authentication.class));                      
}
 public static class request_token extends AsyncTask<Void, Void, String[]> {

    static String requesttoken_string;
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    // Will contain the raw JSON response as a string.
    String forecastJsonStr = null;

    @Override
    protected String[] doInBackground(Void... params) {
        try {

            final String FORECAST_BASE_URL =
                    "http://api.themoviedb.org/3/";

            final String movie_info_url = FORECAST_BASE_URL + "authentication/token/new";
            final String QUERY_PARAM = "api_key";


            Uri builtUri = Uri.parse(movie_info_url).buildUpon()
                    .appendQueryParameter(QUERY_PARAM, BuildConfig.MOVIES_DB_API_KEY)
                    .build();

            URL url = new URL(builtUri.toString());


            // Create the request to OpenWeatherMap, and open the connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                // Nothing to do.
                return null;
            }

            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty.  No point in parsing.
                return null;
            }
            forecastJsonStr = buffer.toString();


        } catch (IOException e) {
            Log.e("Fetch Movie Data", "Error ", e);
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e("Fetch Movie Data", "Error closing stream", e);
                }
            }
        }
        Log.v("JSON", forecastJsonStr);
        getJsonRequestToken_data(forecastJsonStr);
        return null;
    }

    public void getJsonRequestToken_data(String data) {
        try {
            JSONObject json_requesttoken_object = new JSONObject(data);
            requesttoken_string =   json_requesttoken_object.getString("request_token").toString();
            Log.v("Token", requesttoken_string);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

身份验证class:

public class authentication extends AppCompatActivity
{
Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("sdfsdf","sdffdf");
    setContentView(R.layout.web_view);
    WebView webView=(WebView)findViewById(R.id.webView_id);
    webView.loadUrl("https://www.google.com");
  }
}

当我使用断点时,我发现调用了 request_tmdb 构造函数但是 onCreate() 方法未被调用。

Android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.venkateswara.movies1">

<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"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".request_tmdb"
        android:parentActivityName=".MainActivity"/>
    <activity android:name=".authentication"
        android:parentActivityName=".MainActivity"/>
</application>

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

我正在从 DisplayMovie_Data class 呼叫 request_tmdb。

 public class new_Movie_imgtxt extends AppCompatActivity {
static JSONArray resultsArray;
static JSONObject jsonString;
static String movie_sort="popular";

//Display poster, Title ,popularity and vote average
  public static class fetchMovieData extends AsyncTask<Void, Void, String[]> {

    static public String[] title;
    static public String[] image_url;
    static public double[] popularity;
    static public String[] vote_avg;


    @Override
    protected String[] doInBackground(Void... params) {

        movie_sort=movie_tab;
        // These two need to be declared outside the try/catch
        // so that they can be closed in the finally block.
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        // Will contain the raw JSON response as a string.
        String forecastJsonStr = null;

        try {

            final String FORECAST_BASE_URL =
                    "http://api.themoviedb.org/3/";

            final String movie_info_url=FORECAST_BASE_URL+"movie/"+movie_sort+"?";
            final String QUERY_PARAM = "api_key";


            Uri builtUri = Uri.parse(movie_info_url).buildUpon()
                    .appendQueryParameter(QUERY_PARAM, BuildConfig.MOVIES_DB_API_KEY)
                    .build();

            URL url = new URL(builtUri.toString());



            // Create the request to OpenWeatherMap, and open the connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                // Nothing to do.
                return null;
            }

            reader = new BufferedReader(new InputStreamReader(inputStream));


            String line;
            while ((line = reader.readLine()) != null) {
                // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                // But it does make debugging a *lot* easier if you print out the completed
                // buffer for debugging.
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty.  No point in parsing.
                return null;
            }
            forecastJsonStr = buffer.toString();



        } catch (IOException e) {
            Log.e("Fetch Movie Data", "Error ", e);
            // If the code didn't successfully get the weather title, there's no point in attemping
            // to parse it.
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e("Fetch Movie Data", "Error closing stream", e);
                }
            }
        }
        getJsonData(forecastJsonStr);
        new DisplayMovie_Data(title,image_url,popularity,vote_avg);
        return null;
    }

    @Override
    protected void onPostExecute(String[] strings) {
        super.onPostExecute(strings);
        new DisplayMovie_Data(title,image_url,popularity,vote_avg);

    }

    // Json title is fetched

    public void getJsonData(String forecastJsonString) {

        try {
            jsonString = new JSONObject(forecastJsonString);
            resultsArray = jsonString.getJSONArray("results");
            title = new String[resultsArray.length()];
            popularity=new double[resultsArray.length()];
            vote_avg=new String[resultsArray.length()];
            image_url=new String[resultsArray.length()];
            for (int i = 0; i < resultsArray.length(); i++)
            {
                image_url[i]=resultsArray.getJSONObject(i).getString("poster_path").toString();
                title[i] = resultsArray.getJSONObject(i).getString("original_title").toString();
                popularity[i]=resultsArray.getJSONObject(i).getDouble("popularity");
                vote_avg[i]=resultsArray.getJSONObject(i).getString("vote_average").toString();

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

static String[] result=new String[20];
static Context mcontext;
static String[] image_url1=new String[20];
static double[] popular=new double[20];
static String[] vote=new String[20];
static String movie_tab;
static DecimalFormat df=new DecimalFormat("#.##");

public static class DisplayMovie_Data extends BaseAdapter{

    public DisplayMovie_Data(Context mActivity,String filter) {
        mcontext = mActivity;
        movie_tab=filter;
        new new_Movie_imgtxt.fetchMovieData().execute();
        new request_tmdb();


    }
    public DisplayMovie_Data(String[] data,String[] image_url,double[] popularity1,String[] vote_avg1)
    {
        result=data;
        image_url1=image_url;
        popular=popularity1;
        vote=vote_avg1;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub

        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return result[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder {
        TextView tv;
        TextView tv1;
        TextView tv2;
        ImageView img;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder;
        View view;
        view=convertView;
        if(view==null)
        {
            LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.content_list_text_adapter, null);
            holder = new Holder();
            holder.tv = (TextView) view.findViewById(R.id.main_title_textview);
            holder.tv1=(TextView) view.findViewById(R.id.main_popularity_textview);
            holder.tv2=(TextView) view.findViewById(R.id.main_voteavg_textview);
            holder.img = (ImageView) view.findViewById(R.id.main_content_image);
            view.setTag(holder);
        }
        else
            holder=(Holder)view.getTag();

        holder.tv.setText(result[position]);
        holder.tv1.setText( df.format(popular[position])+"");  //format popularity to two decimal places
        holder.tv2.setText(vote[position]);
        Picasso.with(mcontext).load("http://image.tmdb.org/t/p/w185/"+image_url1[position]).into(holder.img);
        return view;
    }

}
}

由于 request_tmdb 正在扩展 AppCompatActivity 您需要使用和意图启动它。之后把这一行 new request_token().execute();

在您的 request_tmdb activity

oncreate 方法中

您不应在 activity 类

中使用构造函数

为了实现可调试,您可以在AndroidManifest.xml.

中添加如下代码
<application android:icon="@drawable/icon" android:label="@string/app_name" 
android:debuggable="true">

或者您可以在 build.gradle 文件中将 debuggable 设置为 true。

debuggable false