2016-04-21 6 views
0

onCreate nicht aufgerufen, debuggte den Code mit Break-Punkte.onCreate nicht aufgerufen, wenn ich mit Haltepunkten überprüft

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

Authentifizierungs-Klasse:

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

Wenn ich Haltepunkte verwendet, fand ich, dass request_tmdb Konstruktor aufgerufen wird, aber onCreate() Methode wird nicht aufgerufen.

Android Manifest

<?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> 

I request_tmdb von DisplayMovie_Data Klasse nenne. Verwendung und Absicht

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

Wie beginnen Sie 'request_tmdb'? –

+0

Und wenn ich innerhalb einer anderen Methode startActivity() aufrufen, bekomme ich null Zeigerausnahme. Da onCreate() jetzt nicht aufgerufen wird, hoffe ich, dass ich diesen Fehler jetzt nicht bekomme. Kann jemand mir helfen und mir sagen, was das Problem in diesem Code –

+0

@MikeM ist. Ich rufe es von einer anderen Klasse an. –

Antwort

1

Da request_tmdb erweitert AppCompatActivity Sie brauchen, um es zu starten. Danach diese Zeile setzen new request_token().execute();

innerhalb des oncreate Methode Ihrer request_tmdb Aktivität

Sie nicht Konstrukteuren in Aktivitätsklassen verwenden sollten

+0

Danke für Ihre Antwort. –

Verwandte Themen