gpt4 book ai didi

java - 启动应用程序的按钮( fragment )

转载 作者:行者123 更新时间:2023-12-01 12:57:49 24 4
gpt4 key购买 nike

因此,只有 viewpager 与 Activity 一起使用时,我才会遇到更多 fragment 问题。 :|

我的问题来自于实现启动日历应用程序的代码。当然,我有一个链接到代码的按钮,当单击该按钮时,它会启动日历应用程序。我已经使用下面的代码来做到这一点 -

// Calendar Launch
Button calendar_launch = (Button) getView().findViewById(R.id.calendar_launch);
date_launch.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
PackageManager packageManager = getActivity().getPackageManager();
Intent intent_calendar = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);

// Verify calendar implementation
String clockImpls[][] = {
{"Default", "com.android.calendar", "com.android.calendar.AllInOneActivity" },
{"HTC", "com.htc.calendar", "com.htc.calendar.LaunchActivity"},
{"GOOGLE", "com.google.android.calendar", "com.android.calendar.AllInOneActivity"},
{"SAMSUNG", "ShiftCalendar.SamsungElectronics","ShiftCalendar.SamsungElectronics.main"}
};

boolean foundClockImpl = false;

for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
intent_calendar.setComponent(cn);
//debug("Found " + vendor + " --> " + packageName + "/" + className);
foundClockImpl = true;
} catch (NameNotFoundException e) {
//debug(vendor + " does not exists");
}
}

if (foundClockImpl) {
startActivity(intent_calendar);
}
}
});

但是启动应用程序后,它立即关闭,在检查错误日志后,我上网寻找合适的答案,但我真的不知道要搜索什么?查看错误日志,它表示错误 (NullPointerException) 存在于以下行中 - Button date_launch = (Button) getView().findViewById(R.id.calendar_launch);

我必须如何修复这个错误,在将代码从 man Activity 实现到 Fragment 时我还需要知道什么?

类( fragment )

public class HomeFragment extends Fragment {  

public static HomeFragment newInstance(String title) {

HomeFragment homeFragment = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
homeFragment.setArguments(bundle);
return homeFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_activity_home, container, false);

// (Calendar) Date function - Displays dateview on Card
final boolean keepRunning1 = true;
Thread thread_two = new Thread(){
@Override
public void run(){

while(keepRunning1){

// Make the thread wait half a second. If you want...
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Toast.makeText(getActivity().getApplicationContext(), "Default Signature Fail", Toast.LENGTH_LONG).show();
e.printStackTrace();
}

getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
TextView date = (TextView) getView().findViewById(R.id.date);
date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
}
});
}
}
};

thread_two.start();

// Calendar Launch
Button calendar_launch = (Button) getView().findViewById(R.id.calendar_launch);
date_launch.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
PackageManager packageManager = getActivity().getPackageManager();
Intent intent_calendar = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);

// Verify calendar implementation
String clockImpls[][] = {
{"Default", "com.android.calendar", "com.android.calendar.AllInOneActivity" },
{"HTC", "com.htc.calendar", "com.htc.calendar.LaunchActivity"},
{"GOOGLE", "com.google.android.calendar", "com.android.calendar.AllInOneActivity"},
{"SAMSUNG", "ShiftCalendar.SamsungElectronics","ShiftCalendar.SamsungElectronics.main"}
};

boolean foundClockImpl = false;

for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
intent_calendar.setComponent(cn);
//debug("Found " + vendor + " --> " + packageName + "/" + className);
foundClockImpl = true;
} catch (NameNotFoundException e) {
//debug(vendor + " does not exists");
}
}

if (foundClockImpl) {
startActivity(intent_calendar);
}
}
});
return view;
}
}

布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
android:layoutAnimation="@anim/default_anim_parse"
android:orientation="vertical" >

<!-- Header -->
<LinearLayout
android:id="@+id/header_image"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@drawable/header"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/google_search"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp"
android:background="@drawable/mybutton"
android:orientation="vertical" >

<com.activelauncher.MyTextView
android:id="@+id/search_text"
android:layout_width="100dp"
android:layout_height="42dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:gravity="center|left"
android:text="Google Search"
android:textColor="#6b6b6b"
android:textSize="15.5sp" />

<Button
android:id="@+id/search_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:gravity="center|left"
android:background="@drawable/google_search_button" />

</RelativeLayout>
</LinearLayout>
<!-- Header End -->

<!-- Date View -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_card"
android:orientation="vertical" >

<com.activelauncher.MyTextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableRight="@drawable/ic_action_event_normal"
android:gravity="left"
android:text="@string/calendar"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#6b6b6b"
android:textSize="20sp" />

<com.activelauncher.MyTextView
android:id="@+id/date_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="left"
android:text="@string/calendar_summary"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#6b6b6b"
android:textSize="15sp" />
</LinearLayout>
<!-- Date View End -->
<!-- Date Button -->
<Button
android:id="@+id/calendar_launch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/mybutton"
android:drawableLeft="@drawable/ic_action_browse_normal"
android:gravity="left|center"
android:paddingLeft="5dp"
android:text="@string/calendar_button"
android:textColor="#4285F4" />
<!-- Date Button End -->

<!-- Time View -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_card"
android:orientation="vertical" >

<com.activelauncher.MyTextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableRight="@drawable/ic_action_alarm_normal"
android:gravity="left"
android:text="@string/time"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#6b6b6b"
android:textSize="20sp" />

<com.activelauncher.MyTextView
android:id="@+id/time_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="left"
android:text="@string/time_summary"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#6b6b6b"
android:textSize="15sp" />
</LinearLayout>
<!-- Time Button -->

<Button
android:id="@+id/time_launch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/mybutton"
android:drawableLeft="@drawable/ic_action_browse_normal"
android:gravity="left|center"
android:paddingLeft="5dp"
android:text="@string/time_button"
android:textColor="#4285F4" />
<!-- Time View End -->


</LinearLayout>

最佳答案

试试这个方法

Button date_launch = (Button) view.findViewById(R.id.calendar_launch);

而不是

 Button calendar_launch = (Button) getView().findViewById(R.id.calendar_launch); 

您必须使用特定的 ViewFragementonCreateView(....) 中初始化您的 Button > 你已经夸大了

关于java - 启动应用程序的按钮( fragment ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23752045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com