0
我試着用3個片段java代碼不片段活動工作
當我嘗試輸入片段類中的Java代碼創建Android平臺的應用程序,我一直這個錯誤: java.lang.NullPointerException
例如,對於一個簡單的按鈕來改變視圖:
package com.test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class creation extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.creation, container, false);
final Button ok = (Button)getView(). findViewById(R.id.button3);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
Fragment newFragment = new mesvideos();
// consider using Java coding conventions (upper char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.layout.creation, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
return root ;
}
或者與VideoView
:
mVideoView = (VideoView) findViewById(R.id.videoView1);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +
"/" + R.raw.presentation));
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
粘貼完整的堆棧跟蹤(複製logcat中存在錯誤的部分) –
錯誤告訴您它發生的確切路線。所有案例中有99%在該行中有一個「。」。 '.'之前的東西是'null'。在你的情況'getView()' – zapl