lundi 11 mai 2015

Android: App crashes since changing the id-tags of some views (using Eclipse)

Since some week's i am working on an app consisting of 3 activitys. 2 of them uses fragments. There are 31 layout-files (XML) in my project total. All was working fine ...... to yesterday.....

By creating the layouts for the fragment i used the auto-generated id's from eclipse (textView1, textView2 and so on). Yesterday I wanted some id's to rename. But something was going wrong. (i don't check the preview by dialog, so there was changed in other fragments and codeparts too. I try to remove the wrong changes,but it seems i forgot something, or just dont know about something)

All is fine when i toogle the fragment, but if i want to change something (e.g setText() on a TextView) the app crashes. It seems it couldnt find the View ("null object reference") but formally the app has found the View by id.

Here is the Error Log: ( i marked the critical part with a comment )

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

And here is the source of the activity (blanked out some parts):

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;  
        }


}

The Activity layout:

<FrameLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    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>

And here is the fragment_agb1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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>

Hope somebody of you can help me. I couldn't find the problem yet. Sorry for my bad english.

thx.

Aucun commentaire:

Enregistrer un commentaire