gpt4 book ai didi

java - 我无法绑定(bind)到 "View"中的 fragment 。 getView() 不起作用 :(

转载 作者:行者123 更新时间:2023-12-01 10:11:16 25 4
gpt4 key购买 nike

无法创建对象,认为Tab1.java中包含错误

Tab1 中包含计时器。因此,我送你去看“布局”。

Tab.java

package com.android.example;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
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.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;


public class Tab1 extends Fragment {

private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent;
private LinearLayout linearLayout1, linearLayout2;
private Handler handler;
private Runnable runnable;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1, container, false);
initUI();
countDownStart();
return v;
}


@SuppressLint("SimpleDateFormat")
private void initUI() {
linearLayout1 = (LinearLayout) getView().findViewById(R.id.ll1);
linearLayout2 = (LinearLayout) getView().findViewById(R.id.ll2);
tvDay = (TextView) getView().findViewById(R.id.txtTimerDay);
tvHour = (TextView) getView().findViewById(R.id.txtTimerHour);
tvMinute = (TextView) getView().findViewById(R.id.txtTimerMinute);
tvSecond = (TextView) getView().findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) getView().findViewById(R.id.tvevent);
}

// //////////////COUNT DOWN START/////////////////////////
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
// Here Set your Event Date
Date futureDate = dateFormat.parse("2016-12-30");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
tvDay.setText("" + String.format("%02d", days));
tvHour.setText("" + String.format("%02d", hours));
tvMinute.setText("" + String.format("%02d", minutes));
tvSecond.setText("" + String.format("%02d", seconds));
} else {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
tvEvent.setText("Android Event Start");
handler.removeCallbacks(runnable);
// handler.removeMessages(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
}

// //////////////COUNT DOWN END/////////////////////////
}

MainActivity.java然后连接 fragment

package com.android.example;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;

int Numboftabs =3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CharSequence Titles[]= {getResources().getString(R.string.titles), getResources().getString(R.string.result), getResources().getString(R.string.contacts)};


// Creating The Toolbar and setting it as the Toolbar for the activity

toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);


// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});

// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

tab_1.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:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/counter_back"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >

<TextView
android:id="@+id/tvevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:singleLine="true"
android:text="Android Event Start"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:id="@+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/counter_back"
android:gravity="center"
android:orientation="horizontal"
android:visibility="visible" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter_back"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/txtTimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />

<TextView
android:id="@+id/txt_TimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Days"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter_back"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/txtTimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />

<TextView
android:id="@+id/txt_TimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Hour"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter_back"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/txtTimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />

<TextView
android:id="@+id/txt_TimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Minute"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter_back"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/txtTimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />

<TextView
android:id="@+id/txt_TimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Second"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

等级错误:我无法找出错误。请不要“写”,因为我是初学者程序员:)

03-19 18:00:55.154 28193-28193/? E/Zygote: v2
03-19 18:00:55.154 28193-28193/? I/libpersona: KNOX_SDCARD checking this for 10140
03-19 18:00:55.154 28193-28193/? I/libpersona: KNOX_SDCARD not a persona
03-19 18:00:55.154 28193-28193/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-A300F_5.0.2-1_0026
03-19 18:00:55.154 28193-28193/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
03-19 18:00:55.154 28193-28193/? I/art: Late-enabling -Xcheck:jni
03-19 18:00:55.174 28193-28193/? D/TimaKeyStoreProvider: in addTimaSignatureService
03-19 18:00:55.184 28193-28193/? D/TimaKeyStoreProvider: Cannot add TimaSignature Service, License check Failed
03-19 18:00:55.184 28193-28193/? D/ActivityThread: Added TimaKesytore provider
03-19 18:00:55.374 28193-28193/com.android.example D/DisplayManager: DisplayManager()
03-19 18:00:55.714 28193-28240/com.android.example D/OpenGLRenderer: Render dirty regions requested: true
03-19 18:00:55.784 28193-28193/com.android.example W/FragmentManager: moveToState: Fragment state for Tab2{3edefe8b #0 id=0x7f0c0052} not updated inline; expected state 3 found 2
03-19 18:00:55.834 28193-28193/com.android.example D/AndroidRuntime: Shutting down VM
03-19 18:00:55.844 28193-28193/com.android.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.example, PID: 28193
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.android.example.Tab1.initUI(Tab1.java:36)
at com.android.example.Tab1.onCreateView(Tab1.java:28)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:17826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748)
at android.view.View.measure(View.java:17826)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5873)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5536)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.j
03-19 18:00:57.404 28193-28193/com.android.example I/Process: Sending signal. PID: 28193 SIG: 9

最佳答案

我认为你应该将 inflater.inflate() 的结果传递给 initUI()。不确定,但我认为 getView() 返回 null 是因为系统还没有收到您新创建的 View (您刚刚创建了它,但只有从 onCreateView 返回后才会绑定(bind)到 fragment )。

关于java - 我无法绑定(bind)到 "View"中的 fragment 。 getView() 不起作用 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099977/

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