因为有一周我正在开发一个由3个活动组成的应用程序。其中2个使用碎片。我的项目总共有31个布局文件(XML)。一切都很好......到了昨天......
通过创建片段的布局,我使用了eclipse中自动生成的id(textView1,textView2等)。昨天我想要一些id重命名。但是出了点问题。 (我没有通过对话框检查预览,因此其他片段和代码部分也发生了变化。我尝试删除错误的更改,但似乎我忘记了某些内容,或者只是不知道某些内容)
当我对片段进行设置时,一切都很好,但如果我想要更改某些内容(例如TextView上的setText())应用程序崩溃。它似乎无法找到View(“空对象引用”),但正式的应用程序已经找到了ID的视图。
这是错误日志:(我用评论标记了关键部分)
05-11 08:37:22.753: E/AndroidRuntime(6875): FATAL EXCEPTION: main
05-11 08:37:22.753: E/AndroidRuntime(6875): Process: com.nobody.allstuffapp, PID: 6875
05-11 08:37:22.753: E/AndroidRuntime(6875): java.lang.IllegalStateException: Could not execute method of the activity
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.view.View$1.onClick(View.java:4020)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.view.View.performClick(View.java:4780)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.view.View$PerformClick.run(View.java:19866)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.os.Handler.handleCallback(Handler.java:739)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.os.Handler.dispatchMessage(Handler.java:95)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.os.Looper.loop(Looper.java:135)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.app.ActivityThread.main(ActivityThread.java:5254)
05-11 08:37:22.753: E/AndroidRuntime(6875): at java.lang.reflect.Method.invoke(Native Method)
05-11 08:37:22.753: E/AndroidRuntime(6875): at java.lang.reflect.Method.invoke(Method.java:372)
05-11 08:37:22.753: E/AndroidRuntime(6875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-11 08:37:22.753: E/AndroidRuntime(6875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-11 08:37:22.753: E/AndroidRuntime(6875): Caused by: java.lang.reflect.InvocationTargetException
05-11 08:37:22.753: E/AndroidRuntime(6875): at java.lang.reflect.Method.invoke(Native Method)
05-11 08:37:22.753: E/AndroidRuntime(6875): at java.lang.reflect.Method.invoke(Method.java:372)
05-11 08:37:22.753: E/AndroidRuntime(6875): at android.view.View$1.onClick(View.java:4015)
05-11 08:37:22.753: E/AndroidRuntime(6875): ... 10 more
05-11 08:37:22.753: E/AndroidRuntime(6875): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
05-11 08:37:22.753: E/AndroidRuntime(6875): at com.nobody.allstuffapp.LoginActivity.AGB1Show(LoginActivity.java:338)
05-11 08:37:22.753: E/AndroidRuntime(6875): ... 13 more
以下是活动的来源(删除了一些部分):
package com.nobody.allstuffapp;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import org.json.JSONArray;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import com.nobody.allstuffapp.db.AppStatus;
import com.nobody.allstuffapp.internet.RequestGetTask;
public class LoginActivity extends ActionBarActivity {
public int shownfragment;
private Context mContext;
Bundle extras = new Bundle();
private String ex_userName;
private String ex_userEmail;
private String ex_userPW;
private boolean ex_userKeepLogged;
private static String nameOfUserSetting = "LastUser.log";
private static String nameOfAGBFile = "Usertherms.txt";
private static String nameOfDRightsFile = "Datarights.txt";
private static String nameOfHelpFile = "Help.txt";
private File appFilesPath;
private File UserLogFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
toolbar.setBackgroundColor(getResources().getColor(R.color.BaseColor));
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.login_contentframe, new LogonFragment()).commit();
shownfragment = 0;
}
mContext = this;
// get extra strings
extras = getIntent().getExtras();
ex_userName = extras.getString("userName");
ex_userEmail = extras.getString("userEmail");
ex_userPW = extras.getString("userPW");
ex_userKeepLogged = Boolean.valueOf((extras.getString("userKeepLogged")));
// get files-dir
appFilesPath = mContext.getFilesDir();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
// Fragment Logon Creator
public class LogonFragment extends Fragment {
public LogonFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
}
// Fragment Registrationsformular Creator
public static class RegisterFragment extends Fragment {
public RegisterFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_register, container, false);
return rootView;
}
}
// Fragment Nutzerbestimmungen Createtor
public static class AGB1Fragment extends Fragment {
public AGB1Fragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_agb1, container, false);
return rootView;
}
}
// Fragment Datenverwendungsrichtlinien Createtor
public static class AGB2Fragment extends Fragment {
public AGB2Fragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_agb2,container, false);
return rootView;
}
}
// Logon Screen einblenden
public void HomeShow(View view){
getSupportFragmentManager().beginTransaction()
.replace(R.id.login_contentframe, new LogonFragment())
.commit();
this.setTitle(R.string.title_activity_login);
shownfragment = 0;
}
// Registrationsformular einblenden
public void RegisterMe(View view) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.login_contentframe, new RegisterFragment())
.commit();
this.setTitle(R.string.str_register_title);
shownfragment = 1;
}
private void hideKeyboard() {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
// Nutzerbestimmungen einblenden
public void AGB1Show(View view) {
AGB1Fragment agb1Fragment = new AGB1Fragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.login_contentframe, agb1Fragment)
.commit();
this.setTitle(R.string.str_login_usertherms);
// ================= this will cause a crash (log shown below) ====================
((TextView) findViewById(R.id.lay_agb1).findViewById(R.id.tv_agb1_agbcontent)).setText("Hello World!!!");
// ========= same happens without searching for layout id =====================
// = no problems till yesterday. Only the R.id.tv_agb1_agbcontent chanched =
((TextView) findViewById(R.id.tv_agb1_agbcontent)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.tv_agb1_agbcontent)).setText(Html.fromHtml(getResources().getString(R.string.txt_agb1)));
}
// Datenverwendungsrichtlinien einblenden
public void AGB2Show(View view) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.login_contentframe, new AGB2Fragment())
.commit();
this.setTitle(R.string.str_login_datatherms);
shownfragment = 1;
}
}
活动布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context="com.nobody.allstuffapp.LoginActivity" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/mytoolbar" />
<ScrollView
android:id="@+id/login_contentframe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
android:scrollbarStyle="outsideInset" >
</ScrollView>
</FrameLayout>
这是fragment_agb1.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="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/lay_agb1" >
<TextView
android:id="@+id/tv_agb1_agbcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="0dp"
android:text="@string/txt_agb1"
android:textSize="14sp" />
<RelativeLayout
android:id="@+id/lay_agb1_button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginTop="20dp"
android:gravity="center" >
<ImageView
android:id="@+id/iv_agb1_button_back"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:contentDescription="@string/str_cont_register"
android:onClick="HomeShow"
android:scaleType="centerInside"
android:src="@drawable/bz_main_nobanner" />
<TextView
android:id="@+id/tv_agb1_buttoncaption_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/str_login_back"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</RelativeLayout>
<TextView
android:id="@+id/tv_agb1_copyright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@string/str_login_copyright"
android:textSize="12sp" />
</LinearLayout>
希望你们中的某些人可以帮助我。我还没找到问题所在。
抱歉我的英语不好。
THX。
编辑: ====================================== =============================
我想我发现了问题,但如何解决???:
用于解释:如果存在带有名为“TextView1”的TextView的两个布局文件,那么两者在R-File中将具有相同的Resource-ID。如果我在其中一个布局中更改名称,那么它也会更改另一个布局中的名称。如果我使用更新参考对话框的预览,只选择我想要更改的布局和使用引用而不选择要更新的R文件的java文件,那么我产生错误原因。但是如果我选择要更新的r文件,那么另一个布局的Object会以相同的方式丢失他的Reference。对?
现在的问题是:如何重命名textview(通过id)和Eclipse添加一个新的Resource-int,不重命名现有的,现在是否存在其他对象?
答案 0 :(得分:0)
您发布的第二个布局是&#34; fragment_agb1.xml&#34;但是在actvity_login.xml中你已经包含了布局&#34; mytoolbar&#34;。这可能是问题所在。
您可以验证名称吗?
答案 1 :(得分:0)
((TextView的) findViewById(R.id.lay_agb1).findViewById(R.id.tv_agb1_agbcontent))。setText(&#34; Hello World !!!&#34;);
尝试将此行替换为:
((TextView)agb1Fragment.getView().findViewById(R.id.tv_agb1_agbcontent)).setText("Hello World!!!");
修改强>
尝试在public
课程中创建TextView
的{{1}}引用:
AGB1Fragment
然后使用它:
public TextView tv_content;
onCreateView(...) {
...
tv_content = (TextView) rootView.findViewById(R.id.tv_agb1_agbcontent);
...
}