2016-06-05 2 views
-1

Я получаю эту ошибку java.lang.RuntimeException: Невозможно создать экземпляр активность ComponentInfo {com.kosalgeek.android.androidphpmysql/com.kosalgeek.android.androidphpmysql.YoutubeActivity }: java.lang.NullPointerException: Попытка вызвать виртуальный метод 'java.io.Serializable android.content.Intent.getSerializableExtra (java.lang.String)' на нулевой ссылки на объект Там что-то не так сПопытки вызвать виртуальный метод на нулевую ссылку на объект для Video_id

Video_id

. Я получаю значение Video_id из другого класса, который product.java

из следующего сценария

public class YoutubeActivity extends YouTubeBaseActivity implements 
    YouTubePlayer.OnInitializedListener { 
Product product = (Product) getIntent().getSerializableExtra("product"); 
TextView etName, etPrice, etQty, etImageUrl; 
ImageView ivImage; 
String VIDEO_ID; 

private static final int RECOVERY_DIALOG_REQUEST = 10; 
public static final String API_KEY = ""; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_youtube); 

    YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view); 
    youTubeView.initialize(API_KEY, this); 

    etName = (TextView) findViewById(R.id.etName); 
    etPrice = (TextView) findViewById(R.id.etPrice); 
    etQty = (TextView) findViewById(R.id.etQty); 
    if (product != null) { 
     etName.setText(product.name); 
     etPrice.setText("" + product.views); 
     etQty.setText("" + product.v); 
     VIDEO_ID = product.name ; 


    } 


    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 


@Override 
public void onInitializationFailure(YouTubePlayer.Provider provider, 
            YouTubeInitializationResult errorReason) { 
    if (errorReason.isUserRecoverableError()) { 
     errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); 
    } else { 
     String errorMessage = String.format("YouTube Error (%1$s)", 
       errorReason.toString()); 
     Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); 
    } 

} 

@Override 
public void onInitializationSuccess(YouTubePlayer.Provider provider, 
            YouTubePlayer player, boolean wasRestored) { 
    if (!wasRestored) { 
     player.cueVideo(VIDEO_ID); 
    } 
} 

ответ

1

getIntent() будет возвращать нуль, если onCreate метод не вызывается. Вы используете getIntent из инициализатора поля.

Изменить

Product product = (Product) getIntent().getSerializableExtra("product"); 

в

Product product; 

и добавить к onCreate методу

product = (Product) getIntent().getSerializableExtra("product"); 
Смежные вопросы