【问题标题】:Change to Activity from Fragment从 Fragment 更改为 Activity
【发布时间】:2014-11-29 22:52:55
【问题描述】:

我是 Android 编程新手。我有一个问题,我如何才能从片段到活动。 我试图对其进行编码,但我失败了:(('l_places' 是 Fragment Places 的布局,'p_tr_fr' 是可点击的 TableRow)
这是我的片段代码:

package com.fragment.toactivity;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Places extends Fragment{


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.l_places, container, false);//l_places is the layout
     return rootView;
 }

public void goToAttract(View v)
{
    if (v.getId()== R.id.p_tr_fr) {
        Intent intent = new Intent(getActivity(), Listact.class);
        startActivity(intent);
    }
}
   public View onCreateView(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

};
}

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffd4d4d4">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView2" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="#ffffffff">

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_gravity="bottom"
                style="@android:style/Widget.AutoCompleteTextView"
                android:layout_margin="0dp"
                android:id="@+id/p_tr_fr"
                android:onClick="onClick">

                <ImageView
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:id="@+id/imageView2"
                    android:src="@drawable/m_pf"
                    android:scaleType="fitCenter"
                    android:layout_marginLeft="6dp"
                    android:layout_marginTop="6dp"
                    android:layout_marginBottom="6dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/s_shop"
                    android:id="@+id/textView3"
                    android:layout_column="1"
                    android:layout_gravity="center_vertical"
                    android:textStyle="bold"
                    android:textSize="18dp"
                    android:textColor="#ff000000"
                    android:layout_marginLeft="6dp" />
            </TableRow>
        </TableLayout>
    </ScrollView>

</LinearLayout>

当我单击 TableRow 时,应用程序停止。请帮忙! (对不起我的英语)

【问题讨论】:

  • 我忘记写了,Listact的布局是空的,所以我认为错误不会在那里

标签: java android android-intent android-activity android-fragments


【解决方案1】:

这样做,

Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
            getActivity().startActivity(myIntent); 

【讨论】:

    【解决方案2】:

    @NAP 代码应该可以正常工作。如果应用程序停止 - 给我们堆栈跟踪? 可能的原因是您的活动未在 AndroidManifest 中声明。

    你应该有类似的东西

    <activity android:name="yourpackage.Listact" />
    

    &lt;application&gt; 标签之间

    【讨论】:

    • 我真的不知道是什么问题,但是还是不行,虽然我把我的代码改成了@NAP的代码,而且Manifest文件也没有任何问题,我想:
    • 活动>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多