S
stephan0610
Neues Mitglied
- 0
Hallo,
Ich bin noch relativ neu in der Java-Programmierung und habe folgendes Problem:
Ich habe eine App programmiert, welche die aktuellen GPS-Koordinaten in eine .txt-Datei speichert. Diese App funktioniert auch wunderbar. Nun sollen aber noch weitere Funktionen hinzukommen, sodass ich die App in verschiedene Tabs unterteilen will.
Den Code für die Tabs habe ich aus einem Beispiel im Internet, welchen ich leicht verändert habe. Soweil läuft auch alles gut (man kann zwischen den Tabs wechseln und die Buttons drücken).
Nun habe ich die Klasse: "GPSlocation" erstellt, welche die Aufgaben der ursprünglichen App Übernehmen soll (also das auslesen der GPS-Koordinaten). Nur sobald im Konstruktor dieser Klasse:
"locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);"
aufgerufen wird, stürzt die App ab.
Ein Objekt der GPSlocation-Klasse wird erzeugt, wenn man im Tab1 auf den Button klickt. Das schreiben in die txt-Datei habe ich erstmal weggelassen.
Kann mir jemand den Grund und einen Lösungsansatz geben, warum die App abstürzt?
MeineApp.java:
Tab1.java
GPSlocation.java:
PagerAdapter.java:
Android Manifest:
Vielen Dank für die Hilfe.
Ich bin noch relativ neu in der Java-Programmierung und habe folgendes Problem:
Ich habe eine App programmiert, welche die aktuellen GPS-Koordinaten in eine .txt-Datei speichert. Diese App funktioniert auch wunderbar. Nun sollen aber noch weitere Funktionen hinzukommen, sodass ich die App in verschiedene Tabs unterteilen will.
Den Code für die Tabs habe ich aus einem Beispiel im Internet, welchen ich leicht verändert habe. Soweil läuft auch alles gut (man kann zwischen den Tabs wechseln und die Buttons drücken).
Nun habe ich die Klasse: "GPSlocation" erstellt, welche die Aufgaben der ursprünglichen App Übernehmen soll (also das auslesen der GPS-Koordinaten). Nur sobald im Konstruktor dieser Klasse:
"locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);"
aufgerufen wird, stürzt die App ab.
Ein Objekt der GPSlocation-Klasse wird erzeugt, wenn man im Tab1 auf den Button klickt. Das schreiben in die txt-Datei habe ich erstmal weggelassen.
Kann mir jemand den Grund und einen Lösungsansatz geben, warum die App abstürzt?
MeineApp.java:
Code:
package at.theengine.android.samples.swipetabs;
import java.util.List;
import java.util.Vector;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class MeineApp extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private TabHost mTabHost;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
protected Button startbutton;
class TabFactory implements TabContentFactory {
private final Context mContext;
public TabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//restore selected tabpage from state
if (savedInstanceState != null) {
if(savedInstanceState.getString("tab") != null){
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
}
// Intialize viewpager
setContentView (R.layout.main);
this.initializeTabHost();
this.initalizeViewPager();
//GPS-Tab configuration
}
//Ende onCreate
/*
* Saves the currently selected TabPage to state
*/
protected void onSaveInstanceState(Bundle state) {
state.putString("tab", mTabHost.getCurrentTabTag());
}
/*
* Initialzes the viewpager
*/
private void initalizeViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Tab1.class.getName()));
fragments.add(Fragment.instantiate(this, Tab2.class.getName()));
fragments.add(Fragment.instantiate(this, Tab3.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}
/*
* Initializes the tabpages
*/
private void initializeTabHost() {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
MeineApp.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("GPS"));
MeineApp.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Akku"));
MeineApp.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Einstellungen"));
mTabHost.setOnTabChangedListener(this);
}
/*
* Adds a tab to the tabhost
*/
private static void AddTab(MeineApp activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
/*
* sets the viewpagers current item to the item of the current tab page
*/
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
//nothing todo here
}
/*
* Sets current TabPage
*/
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
public void onPageScrollStateChanged(int state) {
//nothing to do here
}
//Ende der Tab-Methoden
}
Code:
package at.theengine.android.samples.swipetabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Tab1 extends Fragment{
boolean dateiaufzeichnunglaeuft = false;
String filename="";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = (LinearLayout)inflater.inflate(R.layout.fragment1, container, false); //Erstellen des Fragments für Tab1
final Button startbutton = (Button) view.findViewById(R.id.GPSstartbutton); //Button in Tab1
final CheckBox checkboxGPSKoordinaten =(CheckBox) view.findViewById(R.id.checkBox1);
final CheckBox checkboxZeit =(CheckBox) view.findViewById(R.id.checkBox2);
final CheckBox checkboxCellId =(CheckBox) view.findViewById(R.id.checkBox3);
final CheckBox checkboxVerbindungsart =(CheckBox) view.findViewById(R.id.checkBox4);
startbutton.setOnClickListener(new View.OnClickListener() { //OnClickListener für Button in Tab1
public void onClick(View v) {
TextView textView1 =(TextView) view.findViewById(R.id.textView1); //textView1 in Tab1
if(textView1.getText() == ""){
[COLOR=Orange]GPSlocation gpslocation = new GPSlocation();[/COLOR]
textView1.setText(gpslocation.getfilename());
startbutton.setText("Deaktivieren");
checkboxGPSKoordinaten.setEnabled(false);
checkboxZeit.setEnabled(false);
checkboxCellId.setEnabled(false);
checkboxVerbindungsart.setEnabled(false);
}else
{
textView1.setText("");
startbutton.setText("Aktivieren");
checkboxGPSKoordinaten.setEnabled(true);
checkboxZeit.setEnabled(true);
checkboxCellId.setEnabled(true);
checkboxVerbindungsart.setEnabled(true);
}
}
});
return view;
}
}
Code:
package at.theengine.android.samples.swipetabs;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;
import android.widget.Toast;
public class GPSlocation extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
public LocationManager locationManager;
public TelephonyManager tm;
public GsmCellLocation GSMlocation;
public int cid;
boolean dateiaufzeichnunglaeuft = false;
String filename="filename GPSclass nicht geändert";
protected GPSlocation (){
Savefile savefile = new Savefile();
filename=savefile.filename;
[COLOR=Red]locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,new MyLocationListener());
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);[/COLOR]
}
protected String getfilename()
{
return filename;
}
protected void onCreate(Bundle savedInstanceState){
// Toast.makeText(GPSlocation.this,"onCreate lief durch...", Toast.LENGTH_LONG).show();
}
protected String getlatitude(Location location){
return String.valueOf(location.getLatitude());
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
GSMlocation = (GsmCellLocation) tm.getCellLocation();
cid = GSMlocation.getCid();
TextView text = (TextView) findViewById(R.id.textView2);
text.setText("Latitude: "+ String.valueOf(location.getLatitude()) + ",\nLongitude: " + String.valueOf(location.getLongitude())+"\nCell Id: "+String.valueOf(cid));
if(dateiaufzeichnunglaeuft==true)
{
Savefile file= new Savefile();
text.setText("Latitude: "+ String.valueOf(location.getLatitude()) + ",\nLongitude: " + String.valueOf(location.getLongitude())+"\n"+ "Datei: " +filename+"\nNetworktype:"+String.valueOf(tm.getNetworkType()) +"\nCell Id:"+String.valueOf(cid));
file.SaveGPSfile(String.valueOf(location.getTime()) + ":" + String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude())+":" +String.valueOf(tm.getNetworkType()) +":" + String.valueOf(cid)+"\n");
}
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(GPSlocation.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(GPSlocation.this,"Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(GPSlocation.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
}
Code:
package at.theengine.android.samples.swipetabs;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.theengine.android.samples.swipetabs"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permisssion.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permissoin.PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MeineApp"
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>