所以我下载了最新的android sdk和eclipse,现在有了新的东西....:
当我创建一个Activity和一个Layout时,它会生成2个布局文件,例如:main_laout.xml和fragment_main.xml
然而,eclipse仅打开片段文件,我在那里制作了GUI。当我启动我的应用程序时,所有我的按钮和TextView都在那里。我按一个按钮,第二个活动开始。
这里我的问题:第二个活动就像第一个活动(2个布局的xml文件,但这里称为状态)
当iam尝试更改TextView时,我得到一个nullPointer异常。任何人都可以帮我解决这个让我疯狂的问题。
我的代码到目前为止:
statusActivity:
public class StatusActivity extends ActionBarActivity{
private TextView version,dbstatus,dbrows;
private Button done,refresh;
NetworkTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
version=(TextView) findViewById(R.id.versionsOutputTextSTATUS);
dbstatus=(TextView) this.findViewById(R.id.dbstatusOutputTextSTATUS);
dbrows=(TextView) this.findViewById(R.id.dbRowsOutputTextSTATUS);
done=(Button) this.findViewById(R.id.beendenButtonSTATUS);
refresh=(Button) this.findViewById(R.id.refreshButtonSTATUS);
version.setText("Test");
}
我的xml文件: activity_status.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.StatusActivity"
tools:ignore="MergeRootFrame" />
fragment_status.xml:
<RelativeLayout 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"
android:background="@drawable/eisblumen"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.project.zigbeecontrol.StatusActivity$PlaceholderFragment" >
<TextView
android:id="@+id/welcomeTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="@string/status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/versionsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/versionTextSTATUS"
android:layout_alignBottom="@+id/versionTextSTATUS"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/versionTextSTATUS"
android:text="@string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/dbstatusOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/dbstatusTextSTATUS"
android:layout_alignLeft="@+id/versionsOutputTextSTATUS"
android:text="@string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/beendenButtonSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/welcomeTextSTATUS"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/welcomeTextSTATUS"
android:background="@drawable/button_trans"
android:text="@string/endeStatus"
android:onClick="onClickStatus" />
<TextView
android:id="@+id/dbRowsTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/beendenButtonSTATUS"
android:layout_alignRight="@+id/dbstatusTextSTATUS"
android:layout_marginBottom="26dp"
android:text="@string/dbrows"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/dbstatusTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/beendenButtonSTATUS"
android:text="@string/datenbankstatus"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/versionTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/dbstatusTextSTATUS"
android:layout_below="@+id/welcomeTextSTATUS"
android:layout_marginTop="56dp"
android:text="@string/version"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/dbRowsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/dbRowsTextSTATUS"
android:layout_alignBottom="@+id/dbRowsTextSTATUS"
android:layout_alignLeft="@+id/dbstatusOutputTextSTATUS"
android:text="@string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/refreshButtonSTATUS"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/welcomeTextSTATUS"
android:layout_marginBottom="17dp"
android:layout_marginLeft="31dp"
android:layout_toRightOf="@+id/welcomeTextSTATUS"
android:text="@string/refresh"
android:onClick="onClickStatus" />
</RelativeLayout>
即使在它的工作中我发布了&#34; main&#34;的部分内容。程序也是如此,你可以看到我在那里工作: MainActivity:
public class MainActivity extends ActionBarActivity{
//Buttons
private Button beendenButton;
private Button statusbutton;
private Button restartButton;
private Button auswertungButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
/*Zuweißung der Buttons */
beendenButton=(Button) this.findViewById(R.id.endButtonMAIN);
statusbutton=(Button) this.findViewById(R.id.statusButtonMAIN);
restartButton=(Button) this.findViewById(R.id.restartButtonMAIN);
auswertungButton=(Button)this.findViewById(R.id.auswertungButtonMAIN);
/*Fertig mit initzialisieren warten auf Eingabe*/
}
private void statusMethod() {
try{
Intent intent = new Intent(this, StatusActivity.class);
startActivity(intent);
this.finish();
}catch(Exception e){errorMessage();}
}
//.... Some uninterresting Stuff here
public void onClick(View v)
{
switch (v.getId())
{
/*Auswahl was gedrückt wurde und aufruf der Entsprechenden Methode */
case R.id.endButtonMAIN: endActivity(); break;
case R.id.statusButtonMAIN: statusMethod();break;
case R.id.restartButtonMAIN: restartMethod();break;
case R.id.auswertungButtonMAIN: sensorMethod(); break;
default: break;
}
}
}
布局中的两个xmls:
activity_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.MainActivity"
tools:ignore="MergeRootFrame"
/>
最后但并非最不重要的是fragment_main:
<RelativeLayout 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"
android:background="@drawable/eisblumen"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.project.zigbeecontrol.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/endButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/welcomeTextMAIN"
android:background="@drawable/button_trans"
android:text="@string/beenden"
android:onClick="onClick" />
<TextView
android:id="@+id/welcomeTextMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/endButtonMAIN"
android:layout_centerHorizontal="true"
android:text="@string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/statusButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/welcomeTextMAIN"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:background="@drawable/button_trans"
android:text="@string/status"
android:onClick="onClick" />
<Button
android:id="@+id/restartButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/statusButtonMAIN"
android:layout_below="@+id/statusButtonMAIN"
android:layout_marginTop="19dp"
android:background="@drawable/button_trans"
android:text="@string/neustart"
android:onClick="onClick" />
<Button
android:id="@+id/auswertungButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/restartButtonMAIN"
android:layout_below="@+id/restartButtonMAIN"
android:layout_marginTop="33dp"
android:background="@drawable/button_trans"
android:text="@string/sensoren"
android:onClick="onClick" />
</RelativeLayout>
所以PLZ!为什么我在尝试时会得到NPE:
version.setText("Test");
问候
答案 0 :(得分:0)
试试这个..
改变这个..
setContentView(R.layout.activity_status);
到
setContentView(R.layout.fragment_status);
因为TextView
和Button
位于fragment_status.xml
,所以setContentView
应该引用fragment_status.xml
和删除
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
与MainActivity.java
改变这个..
setContentView(R.layout.activity_main);
到
setContentView(R.layout.fragment_main);
和删除
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
答案 1 :(得分:0)
这是你的问题
setContentView(R.layout.activity_status);
但是您的文本视图不存在于activity_status.xml中。在activity_status.xml中创建视图而不是 fragment_status.xml并删除
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}