所以我一直在这里寻找解决方案/原因我的应用程序崩溃。另外我的logcat不起作用。当它确实有效时,它报告主要活动有问题,但我提供了所有代码。如果有人能帮助我,我们将不胜感激。
mainActivity:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gridview = (GridView) findViewById(R.id.gridView1);
Table t=new Table((Double) null, null);
gridview.setAdapter(new ImageAdapter(this, t));
gridview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(gridview.getContext(), "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter:
package com.example.myspending;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<String> texts;
public ImageAdapter(Context context, Table t)
{
mContext=context;
for(int i=0; i<t.size(); i++)
{
texts.add((t.table.get(i).name)+" $"+(t.table.get(i).money));
}
}
@Override
public int getCount()
{
return texts.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView==null){
tv=new TextView(mContext);
tv.setLayoutParams(new GridView.LayoutParams(85,85));
}
else{
tv=(TextView)convertView;
}
tv.setText(texts.get(position));
tv.setTextSize(30);
return tv;
}
}
表:
package com.example.myspending;
import java.util.*;
public class Table
{
public ArrayList<spending> table= new ArrayList<spending>();
double price;
String name;
double total;
public Table(double p, String n)
{
price=p;
name=n;
total+=price;
table.add(new spending(total, "Total"));
table.add(new spending(price, name));
}
public void insert(double p, String n)
{
total+=p;
table.add(new spending(p, n));
}
public void delete(String n)
{
for(int i=0; i<table.size(); i++)
{
if(table.get(i).name==n)
{
total-=table.get(i).money;
table.remove(i);
}
}
}
public void reset()
{
for(int i=0; i<table.size(); i++)
{
table.remove(i);
}
}
public int size()
{
return table.size();
}
}
class spending
{
public double money;
public String name;
public spending(double m, String n)
{
money=m;
name=n;
if(!(name=="deposit"||name=="Deposit"||name=="add"||name=="Add"||name=="added"||name=="Added"))
{
money=money*(-1);
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myspending"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myspending.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>
</application>
</manifest>
布局:
<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: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=".MainActivity" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="325sp"
android:layout_toRightOf="@+id/textView1"
android:numColumns="3" >
</GridView>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/gridView1"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/gridView1"
android:layout_below="@+id/editText1"
android:text="add" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
答案 0 :(得分:0)
class spending
{
public double money;
public String name;
public spending(double m, String n)
{
money=m;
name=n;
if(name.equalsIgnoreCase("deposit") == false &&
name.equalsIgnoreCase("add") == false &&
name.equalsIgnoreCase("added") == false)
{
money *= -1;
}
}
}
答案 1 :(得分:0)
拿出你的(Double) null
,你想在那里完成什么?
此外,使用项目的上下文,而不是gridview
的
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gridview = (GridView) findViewById(R.id.gridView1);
Table t = new Table(-1d, null);
gridview.setAdapter(new ImageAdapter(this, t));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(v.getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
}
});
}
}
答案 2 :(得分:0)
一些一般性评论: