gpt4 book ai didi

java - 当类扩展了LinearLayout时如何将类作为 Activity 调用

转载 作者:行者123 更新时间:2023-12-02 07:15:03 25 4
gpt4 key购买 nike

正如我的标题所说的那样,我的应用程序在Android Studio(JAVA)中遇到问题,当类扩展了LinearLayout时,如何将类作为 Activity 调用?

我的课是:
public class CustomCalendar extends LinearLayout {
我尝试调用的代码:

 Intent customCalendar = new Intent(MainActivity.this, CustomCalendarActivity.class);
startActivity(customCalendar);

我试图做到这一点:
 public class CustomCalendarActivity extends AppCompatActivity {


CustomCalendar customCalendar;

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

customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
customCalendar.SetUpCalendar();
}
}

我的崩溃是这样的:
 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.kalendorius.CustomCalendar.SetUpCalendar()' on a null object reference
at com.example.kalendorius.MainActivity$2.onClick(MainActivity.java:75)

75行是:
 startActivity(customCalendar);

如何设置日历:

我的kalendorius.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:id="@+id/kalendoriaus_virsus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/soft_blue_green"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp">

<ImageButton
android:id="@+id/atgalBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="@drawable/back" />

<TextView
android:id="@+id/dabartineData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_weight="3"
android:gravity="center"
android:text="Data"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />

那只是我使用那个kalendorius_virsus的一点代码。

完成。我找出了问题所在。我只是忘了为数据库构造函数生成:D谢谢所有尝试提供帮助的人。

最佳答案

问题是
customCalendar =(CustomCalendar)findViewById(R.id.kalendoriaus_virsus);

kalendorias.xml中ID为kalendorias_virsus的groupView LinearLayout不是CustomCalendar,因此始终返回null。

您需要了解如何正确使用Costum View

基本示例CustomView->

public class CustomView extends LinearLayout {

public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.costum, null);

this.addView(view);
}

public void setCustomText(String text){
TextView textview = (TextView) findViewById(R.id.textViewId);
textview.setText(text);
}
}

布局custom.xml->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/textViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/app_name"
android:gravity="center">

</TextView>
</LinearLayout>

activity.xml->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<com.example.myapplication.CustomView
android:id="@+id/customViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity类->
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomView customView = findViewById(R.id.customViewId);
customView.setCustomText("Welcome ");
}
}

关于java - 当类扩展了LinearLayout时如何将类作为 Activity 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62436613/

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