从片段按钮按下启动活动会强制关闭

时间:2017-10-28 11:06:41

标签: java android xml android-fragments

无论如何,从片段按下按钮开始活动?我完全按照youtube中视频中完全相同的项目和变量名称进行了操作。当按下按钮时,我只是继续强制关闭,任何想法?这是我在SubPage0.java中的代码:

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class SubPage0 extends Fragment {


public SubPage0() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_thread_feed, container, false);


    Button button = (Button) rootView.findViewById(R.id.but2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), ProjectInfo.class);
            getActivity().startActivity(intent);
        }
    });

    return rootView;
 }
 }

并且是我的fragment_thread_feed.xml中的代码     

<Button
    android:id="@+id/but2"
    android:layout_width="0dp"
    android:layout_height="61dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:onClick="onClickListener"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

这里是我想从该按钮跳转到的活动类,文件名为ProjectInfo.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class ProjectInfo extends AppCompatActivity {

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


    Bundle bundle = getIntent().getExtras();
    if(bundle!=null){
        if(bundle.getString("some")!=null){
            Toast.makeText(getApplicationContext(),"data :" + bundle.getString("some"), Toast.LENGTH_SHORT).show();
            }

        }
    }

}

我真的是初学者,这些代码是我从教程和视频中收集和整合的代码。请帮忙:(

2 个答案:

答案 0 :(得分:0)

我认为你的问题来自

Intent intent = new Intent(getActivity(), ProjectInfo.class);
            getActivity().startActivity(intent);

另一件事

B undle bundle = getIntent().getExtras();

你传递Extras值的地方? LOL

空指针异常。

答案 1 :(得分:0)

只需从按钮中删除android:onClick="onClickListener"

即可
<Button
    android:id="@+id/but2"
    android:layout_width="0dp"
    android:layout_height="61dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />