尝试在Video_id的空对象引用上调用虚方法

时间:2016-06-05 20:30:55

标签: java android youtube-api

我收到此错误                                                                                    java.lang.RuntimeException:无法实例化活动ComponentInfo {com.kosalgeek.android.androidphpmysql / com.kosalgeek.android.androidphpmysql.YoutubeActivity}:java.lang.NullPointerException:尝试调用虚方法'java.io.Serializable android。在null对象引用上的content.Intent.getSerializableExtra(java.lang.String)'

有问题
  

VIDEO_ID

。 我从另一个类product.java

获取Video_id值

来自以下脚本

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 个答案:

答案 0 :(得分:1)

除非调用getIntent()方法,否则

onCreate将返回null。您正在使用字段初始值设定项中的getIntent

更改

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

Product product;

并将其添加到onCreate方法

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