【问题标题】:Intent in Fragment片段中的意图
【发布时间】:2015-06-29 16:42:49
【问题描述】:

我得到了fragment app。但是fragment_home.xml有一个相对的布局并且有imageviews。我开发了代码sn-p,但是当我点击imageview时,它没有运行intents.imageviews需要-

public class HomeFragment extends Fragment {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

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


    return rootView;
}

我写在这里是为了通过 imageviews 运行意图。因为我看到 java 不支持多个扩展。比如扩展片段、活动实现......等等。

 public class home extends Activity implements View.OnClickListener {

    ImageButton firstModule;
    ImageButton secondModule;
    ImageButton thirdModule;
    ImageButton fourModule;
    ImageButton fiveModule;
    ImageButton sx;
    ImageButton seven;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //init buttons
        firstModule = (ImageButton) findViewById(R.id.imageButton1);
        secondModule = (ImageButton) findViewById(R.id.imageButton2);
        thirdModule = (ImageButton) findViewById(R.id.imageButton3);
        fourModule = (ImageButton) findViewById(R.id.imageButton4);
        fiveModule = (ImageButton) findViewById(R.id.imageButton5);
        sx = (ImageButton) findViewById(R.id.imageButto6);
        seven = (ImageButton) findViewById(R.id.imageButton7);
        firstModule.setOnClickListener(this);
        secondModule.setOnClickListener(this);
        thirdModule.setOnClickListener(this);
        fourModule.setOnClickListener(this);
        fiveModule.setOnClickListener(this);
        sx.setOnClickListener(this);
        seven.setOnClickListener(this);
    }


    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.imageButton1:
                startActivity(new Intent(home.this, MainActivitysld.class));
                break;
            case R.id.imageButton2:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButton3:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButton4:
                startActivity(new Intent(home.this, xxxx.class));
                break;
            case R.id.imageButton5:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButto6:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButton7:
                startActivity(new Intent(home.this, xxx.class));
                break;
        }

    }

编辑:我添加了fragment_home.xml

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/konu" />


        <ImageButton
            android:id="@+id/imageButton7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/nt" />

        <ImageButton
            android:id="@+id/imageButto6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ders" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/soru" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/cikmis" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ozel" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/strateji" />
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • xxx 是片段或活动
  • 只看片段的好教程
  • xxx.classes 名称 xxx.java
  • java中的类应该以大写开头。记住这一点。这样一来,代码就更清晰了。

标签: android android-intent layout fragment


【解决方案1】:

home.this 是什么? 您的片段活动是什么?在 startActivity 中,您应该提供该活动和类对象的上下文。 请交叉检查..并尝试调试,如果您的 onclick 方法被调用。 如果没有,设置 ImageButton.setclickable(true)。 看看以上选项是否有效。

【讨论】:

  • 您的点击方法有效吗?尝试打印日志..看看它是否被调用..
【解决方案2】:
firstModule = (ImageButton) findViewById(R.id.imageButton1);
firstModule .setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
     startActivity(new Intent(home.this, MainActivitysld.class));
    }});

// for n numbers

【讨论】:

  • 我将在哪里将此代码添加到 public HomeFragment extends Fragment{} 或 public home extends Activity 实现 OnClickListener ?
猜你喜欢
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多