最后一个屏幕(活动)不断崩溃

时间:2016-03-12 14:26:18

标签: java android xml eclipse android-layout

我是Android编程的新手,我已经搜索了我的眼睛,以解决我的问题,但我无法在任何地方找到它。我正在构建一个具有3种屏幕布局的应用程序,但我在最后一个屏幕上遇到了问题:

问题:

应用程序启动,2个第一个屏幕工作正常,但是当我转到最后一个屏幕(activity_katag.xml& katagActivity.java)并按“Deal1”时,应用程序停止并给我错误“不幸”,AppName“已停止了。“

LogCat错误文件:

03-12 13:32:12.270: W/dalvikvm(614): threadid=1: thread exiting with uncaught exception (group=0x40a122a0)


03-12 13:32:12.290: E/AndroidRuntime(614): FATAL EXCEPTION: main


03-12 13:32:12.290: E/AndroidRuntime(614): java.lang.NullPointerException


03-12 13:32:12.290: E/AndroidRuntime(614):  at com.appname.fundeals.DealsAdapter.getChildrenCount(DealsAdapter.java:42)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.widget.AbsListView$1.run(AbsListView.java:3533)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.os.Handler.handleCallback(Handler.java:615)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.os.Handler.dispatchMessage(Handler.java:92)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.os.Looper.loop(Looper.java:137)


03-12 13:32:12.290: E/AndroidRuntime(614):  at android.app.ActivityThread.main(ActivityThread.java:4745)


03-12 13:32:12.290: E/AndroidRuntime(614):  at java.lang.reflect.Method.invokeNative(Native Method)


03-12 13:32:12.290: E/AndroidRuntime(614):  at java.lang.reflect.Method.invoke(Method.java:511)


03-12 13:32:12.290: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

03-12 13:32:12.290: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)


03-12 13:32:12.290: E/AndroidRuntime(614):  at dalvik.system.NativeStart.main(Native Method)

的AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appname.fundeals"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".inloggActivity" />

        <activity android:name=".katagActivity" />
    </application>

</manifest>

activity_main.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/bakgrund"
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="com.appname.fundeals.MainActivity" >

<TextView

    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="Random Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />


<TextView 
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="75dp"
    android:layout_marginLeft="10dp"
    android:text="Username:"
    android:textAppearance="?android:attr/textAppearanceMedium" />



    />

<EditText
    android:id="@+id/usernameET"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_marginLeft="35dp"
    android:layout_toRightOf="@+id/textView2"
    android:hint="Användarnamn" >
<requestFocus />
</EditText>

<TextView 

   android:id="@+id/textView3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/textView2"
   android:layout_below="@+id/textView2"
   android:layout_marginTop="40dp"
   android:text="Password:"
   android:textAppearance="?android:attr/textAppearanceMedium" />

  <EditText
      android:id="@+id/passwordET"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/textView3"
      android:layout_alignLeft="@+id/usernameET"
      android:layout_alignRight="@+id/usernameET"
      android:hint="lösenord"
      android:inputType="textPassword" />

   <TextView

      android:id="@+id/attemptsLeftTV"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_alignLeft="@+id/textView3"

      android:layout_below="@+id/textView3"

      android:layout_marginLeft="30dp"

      android:layout_marginTop="48dp"

      android:text="Attempts Left:"

      android:visibility="invisible"

      android:textAppearance="?android:attr/textAppearanceMedium" />



   <TextView

      android:id="@+id/numberOfRemainingLoginAttemptsTV"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_alignRight="@+id/textView1"

      android:layout_alignTop="@+id/attemptsLeftTV"

      android:visibility="invisible" />



   <TextView

      android:id="@+id/loginLockedTV"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_below="@+id/loginBtn"

      android:layout_marginTop="35dp"

      android:layout_centerHorizontal="true"

      android:textAppearance="?android:attr/textAppearanceMedium"

      android:visibility="invisible" />

   <Button
       android:id="@+id/loginBtn"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/numberOfRemainingLoginAttemptsTV"
       android:layout_centerHorizontal="true"
       android:onClick="authenticateLogin"
       android:text="Login" />

   <Button
       android:id="@+id/FacebookBtn"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/loginBtn"
       android:layout_toLeftOf="@+id/usernameET"
       android:onClick="authenticateLogin"
       android:text="Facebook" />

MainActivity.java:

package com.appname.fundeals;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;



import com.appname.fundeals.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {

    public class onClick implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            startActivity(new Intent(MainActivity.this, inloggActivity.class));


        }

    }

    private EditText username;

    private EditText password;
    private Button login;
    private TextView loginLockedTV;
    private Button facebookBtn;
    private TextView attemptsLeftTV;
    private TextView numberOfRemainingLoginAttemptsTV;
    int numberOfRemainingLoginAttempts = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        facebookBtn = (Button) findViewById(R.id.FacebookBtn);
        facebookBtn.setOnClickListener(new onClick());
        setupVariables();
    }

    public void authenticateLogin(View view) {
        if (username.getText().toString().equals("admin") && 
                password.getText().toString().equals("admin")) {
            Toast.makeText(getApplicationContext(), "Hello admin!", 
            Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Seems like you 're not admin!", 
                    Toast.LENGTH_SHORT).show();
            numberOfRemainingLoginAttempts--;
            attemptsLeftTV.setVisibility(View.VISIBLE);
            numberOfRemainingLoginAttemptsTV.setVisibility(View.VISIBLE);
            numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));

            if (numberOfRemainingLoginAttempts == 0) {
                login.setEnabled(false);
                loginLockedTV.setVisibility(View.VISIBLE);
                loginLockedTV.setBackgroundColor(Color.RED);
                loginLockedTV.setText("LOGIN LOCKED!!!");
            }
        }
    }

    private void setupVariables() {
        username = (EditText) findViewById(R.id.usernameET);
        password = (EditText) findViewById(R.id.passwordET);
        login = (Button) findViewById(R.id.loginBtn);

        loginLockedTV = (TextView) findViewById(R.id.loginLockedTV);
        attemptsLeftTV = (TextView) findViewById(R.id.attemptsLeftTV);
        numberOfRemainingLoginAttemptsTV = (TextView) findViewById(R.id.numberOfRemainingLoginAttemptsTV);
        numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
    }



}

activity_inlogg.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bakgrund" >

    <TextView
        android:id="@+id/textViewFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="20dp"
        android:text="Random Text."
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewFirst"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:background="#00000000"
        android:src="@drawable/rkeepklar" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="6dp"
        android:background="#00000000"
        android:src="@drawable/stklarmarkerad" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="4dp"
        android:background="#00000000"
        android:src="@drawable/ndklar" />

</RelativeLayout>

inloggActivity.java(屏幕2):

package com.appname.fundeals;

import com.appname.fundeals.inloggActivity.OnClick;
import com.appname.fundeals.R;
import android.app.Activity;
import android.graphics.Color;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class inloggActivity extends Activity {

    public class OnClick implements OnClickListener {

        @Override
        public void onClick(View v) {

            startActivity(new Intent(inloggActivity.this, katagActivity.class));


        }

    }

    private Button imageButton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inlogg);

        Button imageButton1 = (Button) findViewById(R.id.imageButton1);
        imageButton1.setOnClickListener(new OnClick());

    }

}

activity_katag.xml(SCREEN3):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bakgrund"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" 
    >

    <ExpandableListView 
        android:id="@+id/exp_list"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
        android:divider="#A4C739"
        android:dividerHeight="0.5dp"


        ></ExpandableListView>



</RelativeLayout>

katagActivity.java:

package com.appname.fundeals;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class katagActivity extends Activity {

    private static final HashMap<String, List<String>> Deals_Catagory = null;
    HashMap<String, List<String>> Deals_category;
    List<String> Deals_list;
    ExpandableListView Exp_list;
    DealsAdapter adapter;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_katag);

        Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
        Deals_category = DataProvider.getInfo();
        Deals_list = new ArrayList<String>(Deals_category.keySet());

        adapter = new DealsAdapter(this, Deals_Catagory, Deals_list);
        Exp_list.setAdapter(adapter);

    }


}

DealsAdapter.java:

package com.appname.fundeals;

import java.util.HashMap;
import java.util.List;

import com.appname.fundeals.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class DealsAdapter extends BaseExpandableListAdapter {

    private Context ctx;
    private HashMap<String, List<String>> Deals_Catagory;
    private List<String> Deals_List;

     public DealsAdapter(Context ctx, HashMap<String, List<String>> Deals_Catagory, List<String> Deals_List) {

        this.ctx = ctx;
        this.Deals_Catagory = Deals_Catagory;
        this.Deals_List = Deals_List;


        // TODO Auto-generated constructor stub
    }


    @Override
    public int getGroupCount() {

        return Deals_List.size();
    }

    @Override
    public int getChildrenCount(int arg0) {


        return Deals_Catagory.get(Deals_List.get(arg0)).size();
    }

    @Override
    public Object getGroup(int arg0) {

        return Deals_List.get(arg0);
    }

    @Override
    public Object getChild(int parent, int child) {
        // TODO Auto-generated method stub


        return Deals_Catagory.get(Deals_List.get(parent)).get(child);
    }

    @Override
    public long getGroupId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getChildId(int parent, int child) {



        // TODO Auto-generated method stub
        return child;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView) {
        // TODO Auto-generated method stub

        String group_title = (String) getGroup(parent);
        if(convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.parent_deal1, parentView, false);
        }

        TextView parent_textView = (TextView) convertView.findViewById(R.id.parent_deal1);
        parent_textView.setTypeface(null, Typeface.BOLD);
        parent_textView.setText(group_title);


        return convertView;
    }

    @Override
    public View getChildView(int parent, int child, boolean lastChild, View convertView,
            ViewGroup parentview) {

        String child_title = (String) getChild(parent, child);

        if (convertView == null)
        {
            LayoutInflater  inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.child_deal1, parentview, false);
        }
        // TODO Auto-generated method stub

        TextView child_textView = (TextView) convertView.findViewById(R.id.child_deal1);
        child_textView.setText(child_title);



        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

DataProvider.java:

package com.appname.fundeals;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataProvider {


    public static HashMap<String, List<String>> getInfo() 
    {

        HashMap<String, List<String>> DealsDetails = new HashMap<String, List<String>>();

        List<String> Deal_1 = new ArrayList<String>();
        Deal_1.add("Text");
        List<String> Deal_2 = new ArrayList<String>();
        Deal_2.add("Text");
        List<String> Deal_3 = new ArrayList<String>();
        Deal_3.add("Text");
        List<String> Deal_4 = new ArrayList<String>();
        Deal_4.add("Text");

        DealsDetails.put("Deal 1", Deal_1);
        DealsDetails.put("Deal 2", Deal_2);
        DealsDetails.put("Deal 3", Deal_3);
        DealsDetails.put("Deal 4", Deal_4);
        return DealsDetails;




    }

}

0 个答案:

没有答案