2016-05-23 8 views
0

Ich versuche, Retrofit 2 mit diesem grundlegenden Beispiel zu lernen. Und ich habe ein Problem beim Versuch, serialisierbare Daten von Aktivität zu Aktivität zu übergeben ...RuntimeException: Aktivität kann nicht gestartet werden ComponentInfo, NotFoundException

Haben Sie eine Idee warum?

Dies ist mein Code.

logcat:

05-24 00:21:46.215 18156-18156/com.krystian.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.krystian.weatherapp, PID: 18156 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.krystian.weatherapp/com.krystian.flowerapp.ui.DetailActivity}: android.content.res.Resources$NotFoundException: String resource ID 
#0x2 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
    at android.app.ActivityThread.access$800(ActivityThread.java:156) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:211) 
    at android.app.ActivityThread.main(ActivityThread.java:5373) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2 
    at android.content.res.Resources.getText(Resources.java:340) 
    at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 
    at android.widget.TextView.setText(TextView.java:4171) 
    at com.krystian.flowerapp.ui.DetailActivity.setData(DetailActivity.java:52) 
    at com.krystian.flowerapp.ui.DetailActivity.onCreate(DetailActivity.java:47) 
    at android.app.Activity.performCreate(Activity.java:5990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)  
    at android.app.ActivityThread.access$800(ActivityThread.java:156)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:211)  
    at android.app.ActivityThread.main(ActivityThread.java:5373)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:372)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

MainActivity.java:

package com.krystian.flowerapp.ui; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 

import com.krystian.flowerapp.R; 
import com.krystian.flowerapp.adapter.FlowerAdapter; 
import com.krystian.flowerapp.controller.RestManager; 
import com.krystian.flowerapp.helper.Constants; 
import com.krystian.flowerapp.model.Flower; 

import java.util.List; 

import butterknife.BindView; 
import butterknife.ButterKnife; 
import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 

public class MainActivity extends AppCompatActivity implements FlowerAdapter.FlowerClickListener { 
@BindView(R.id.recyclerView) 
RecyclerView recyclerView; 

private RestManager mRestManager; 
private FlowerAdapter mFlowerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ButterKnife.bind(this); 

    configViews(); 

    mRestManager = new RestManager(); 
    Call<List<Flower>> listCall = mRestManager.getFlowerService().getAllFlowers(); 
    listCall.enqueue(new Callback<List<Flower>>() { 
     @Override 
     public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) { 
      if (response.isSuccessful()) { 
       List<Flower> flowerList = response.body(); 

       for (int i=0; i < flowerList.size(); i++) { 
        Flower flower = flowerList.get(i); 
        mFlowerAdapter.addFlower(flower); 
       } 
      } else { 
       int ac = response.code(); 
       Log.e("Response code", String.valueOf(ac)); 
      } 
     } 

     @Override 
     public void onFailure(Call<List<Flower>> call, Throwable t) { 

     } 
    }); 
} 

private void configViews() { 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool()); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false)); 

    mFlowerAdapter = new FlowerAdapter(this); 

    recyclerView.setAdapter(mFlowerAdapter); 
} 

@Override 
public void onClick(int position) { 
    Flower selectedFlower = mFlowerAdapter.getSelectedFlower(position); 
    Intent intent = new Intent(MainActivity.this, DetailActivity.class); 
    intent.putExtra(Constants.REFERENCE.FLOWER, selectedFlower); 
    startActivity(intent); 
} 
} 

DetailActivity.java:

package com.krystian.flowerapp.ui; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.krystian.flowerapp.R; 
import com.krystian.flowerapp.helper.Constants; 
import com.krystian.flowerapp.model.Flower; 
import com.squareup.picasso.Picasso; 

import butterknife.BindView; 
import butterknife.ButterKnife; 

/** 
* Created by: Krystian 
* Date: 23.05.2016. 
*/ 

public class DetailActivity extends AppCompatActivity { 
@BindView(R.id.photoImageView) 
ImageView photoImageView; 
@BindView(R.id.nameTextView) 
TextView nameTextView; 
@BindView(R.id.idTextView) 
TextView idTextView; 
@BindView(R.id.categoryTextView) 
TextView categoryTextView; 
@BindView(R.id.priceTextView) 
TextView priceTextView; 
@BindView(R.id.instructionsTextView) 
TextView instructionsTextView; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_detail); 

    ButterKnife.bind(this); 

    Intent intent = getIntent(); 
    Flower flower = (Flower) intent.getSerializableExtra(Constants.REFERENCE.FLOWER); 

    setData(flower); 
} 

private void setData(Flower flower) { 
    nameTextView.setText(flower.getName()); 
    idTextView.setText(flower.getProductId()); 
    categoryTextView.setText(flower.getCategory()); 
    priceTextView.setText(String.valueOf(flower.getPrice())); 
    instructionsTextView.setText(flower.getInstructions()); 

    Picasso 
      .with(getApplicationContext()) 
      .load(Constants.HTTP.PHOTO_URL + flower.getPhoto()) 
      .into(photoImageView); 
} 
} 

AndroidManifest.xml:

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

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

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

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name="com.krystian.flowerapp.ui.DetailActivity" /> 
</application> 

+1

Was ist in Zeile 52 in DetailActivity? 'idTextView.setText (flower.getProductId());'? Das sieht verdächtig aus. Zeigen Sie uns Ihr Layout-XML für DetailActivity. – t0mm13b

+0

@ t0mm13b Sie haben Recht! Aber warum zeigt diese Methode (die 'int' zurückgibt) keine Unterstreichung, wenn ich' String.valueOf() 'vergesse? 'getPrice()' Methode hat es getan ... – y07k2

+0

Haben eine Antwort zu erklären, warum. – t0mm13b

Antwort

1

Die Linie, die das Problem verursacht ist, auf der Leitung 52 in DetailActivity:

idTextView.setText(flower.getProductId()); 

Die Deklaration setText wird:

Hohlraum setText (int resid)

Der Hinweis ist im Parameternamen, es ist eine Ressource ID, wenn flower.getProductId() übergeben wird in der setText Methode der Ansicht, war es eine Zeichenfolge Ressource in der res/strings.xml oder eine Kennung, die eine Zeichenfolge Ressource, die eine int ist. Und die Verwirrung trat zwischen einer Ressourcen-ID und dem tatsächlichen Getter/Verfahren getProductId() der Klasse flower auf.

Der einfachste Weg, um es zu lösen, ist die valueOf Methode der String Klasse, die die Produkt ID von flower Klasse in einen String-Typ konvertieren wird.

idTextView.setText(String.valueOf(flower.getProductId()));