我是编程和android dev的新手,我正在尝试在Fragment中实现一个按钮,它将打开一个新的Activity。
我收到错误:
无法解析方法'findViewById(int)'我也得到了 错误:无法解析使用者 “意图(com.hashmi.omar.store.Ip6_Cab, java.lang.Class中)'
以下是Ip6_Cab.java:
package com.hashmi.omar.vodafonestore;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by User on 29/06/2015.
*/
public class Ip6_Cab extends Fragment implements View.OnClickListener {
Button button_ip6_24m_b2;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.ip6_cab,container,false);
return v;
//Sets up iphone 6 button
button_ip6_24m_b2 = (Button) findViewById(R.id.button_ip6_24m_b2);
button_ip6_24m_b2.setOnClickListener(this);
}
private void title_ip6_24m_b2_payment() {
startActivity(new Intent(Ip6_Cab.this, Ip6_24m_b2_payment.class));
}
public void onClick(View v) {
switch (v.getId()){
case R.id.button_ip6_24m_b2:
title_ip6_24m_b2_payment();
break;
}
}
}
下面是我想要打开Activity的按钮的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="1dp"
android:paddingRight="1dp"
tools:context="com.hashmi.omar.store.Picker"
android:background="#ffffffff"
android:weightSum="1">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
/Image, Bundle and Button 1
<ImageView
android:layout_width="360dp "
android:layout_height="267dp"
android:id="@+id/imageView"
android:background="@drawable/ip6_24m_b1"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
<Button
android:layout_width="54dp"
android:layout_height="28dp"
android:id="@+id/button_ip6_24m_b1"
android:background="@drawable/button_select_for_bundles"
android:layout_marginTop="5dp"
android:layout_gravity="bottom|center_horizontal" />
/Image, Bundle and Button 2
<ImageView
android:layout_width="360dp "
android:layout_height="425dp"
android:id="@+id/imageView2"
android:background="@drawable/ip6_24m_b2"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
<Button
android:layout_width="54dp"
android:layout_height="28dp"
android:id="@+id/button_ip6_24m_b2"
android:background="@drawable/button_select_for_bundles"
android:layout_marginTop="5dp"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
</ScrollView>
有人可以帮忙吗?
答案 0 :(得分:1)
您必须在布局中找到视图。在片段中,您需要在findViewById
之前添加视图。
此外,如果您致电return
,方法将在此处结束。你必须在方法结束时调用它......
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.ip6_cab,container,false);
//Sets up iphone 6 button
button_ip6_24m_b2 = (Button) v.findViewById(R.id.button_ip6_24m_b2);
button_ip6_24m_b2.setOnClickListener(this);
return v;
}
关于开始新活动,您必须:
getActivity().startActivity(new Intent(getActivity(), Ip6_24m_b2_payment.class));
答案 1 :(得分:0)
更改
ls -l
至startActivity(new Intent(Ip6_Cab.this, Ip6_24m_b2_payment.class));
这是因为startActivity方法仅存在于Context中,而Fragment不会扩展Context。因此,您需要使用父活动来启动新活动。同样理由为什么Intent类的第一个参数必须是getActivity(),应该是一个Context。
您可能希望将getActivity().startActivity(new Intent(getActivity(), Ip6_24m_b2_payment.class));
更改为button_ip6_24m_b2 = (Button) findViewById(R.id.button_ip6_24m_b2);
您想在视图中找到刚膨胀的视图。