2016-04-21 24 views
0

onCreate沒有被調用,用斷點調試代碼。onCreate沒有被調用,當我檢查斷點時

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()方法是不叫:

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(); 
     } 
    } 
} 

認證類。

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類調用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; 
    } 

} 
} 
+0

你是如何開始'request_tmdb'? –

+0

如果我在任何其他方法內調用startActivity(),我收到空指針異常。由於onCreate()現在沒有被調用,所以我希望現在我沒有得到這個錯誤。有人能幫我解決這個問題 –

+0

@MikeM有什麼問題。我從另一個班級打電話給我。 –

回答

1

由於request_tmdb是擴大你需要使用和意圖來啓動它AppCompatActivity。在此之後,就把這行new request_token().execute();

request_tmdb活動

你不應該用在活動類的構造函數的oncreate方法中

+0

感謝您的回覆。 –

相關問題